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():
    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
in Engine by (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.

by (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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.