0 votes

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():
    randomize()
    #get_children()
    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)]
    pass

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
    pass
asked Dec 7, 2019 in Engine by Knollewolle (14 points)

1 Answer

0 votes

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.

answered Dec 8, 2019 by JestersGhost (41 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.