GDscript nodes can't be found (childrens of the scripted node)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Knollewolle

I have put some nodes in a list.
Those nodes are children of the node which i’m making a script for.
The parent node shall randomly choose one of its children and print it on the screen.
Then the children_node shall move to a direction and change it when a button is pressed (not in the script, yet).

extends Node2D
func _ready():
	pass # Replace with function body.
# Nodes arent found!
#var child_nodes = get_children()
var ls_food = [$fd_pizza, $fd_cocoa, $fd_schnitzel, 
$fd_lasagna,$fd_energy, $fd_apple, $fd_cactus, $fd_burger, 
$fd_wine, $fd_cake, $fd_icecone, $fd_ham, $fd_soup, 
$fd_salad, $fd_chicken, $fd_fish, $fd_brokkoli, $fd_bcake, $fd_fruit, $fd_beer]
#var pts_food = {}
var food = ls_food[0]
const SCREEN_WIDTH = 360
const SCREEN_HEIGHT = 640
const MOVE_SPEED = 100

func choose_food():
	food = ls_food[rand_range(0,20)]

func _process(delta):
#	var pos_food = food.position
	food.position.x += (delta*MOVE_SPEED)
	if food.position.x == (SCREEN_WIDTH - 32):
		food.position.y += (delta*MOVE_SPEED)
	if food.position >= 64:
		food.position.x -= (delta*MOVE_SPEED)
#	if food.position.x <= 0 and food.position.y >= 64:
#		queue_free()
#		score -= 10
:bust_in_silhouette: Reply From: JestersGhost

Looks to me, assuming the indentation is correct, that you’re trying to build a list of children outside of any methods - ie. before the node is created, and therefore before it has children.

Declare var child_nodes = [] as an empty array, but then in the _ready method fill it with the get_children call.

Thank you, it worked. Somewhere i have read that the _ready method isn’t needed anymore and is just there for functions like random().

Knollewolle | 2019-12-08 17:09