get_child is not returning the proper object

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

Hello, I am creating a turn-based game where cars move in turns. One movement is one turn, and then the next car moves. The relevant tree is as such:

TurnQueue (Node2D)
- Car 1 (Kinematic Body)
- Car 2 (Kinematic Body)

Node2D contains the turn queue script. My turn queue script is as such:

extends Node2D

class_name TurnQueue

var active_character

func initialize():
	active_character = get_child(0)

func play_turn():
	yield(active_character, "move_completed")
	var new_index : int = (active_character.get_index() + 1) % get_child_count()
	active_character = get_child(new_index)

However, “active_character” returns a value of null, and therefore the play_turn function cannot execute whatsoever. The yield part of play_turn() is meant to take a signal from the first car after moving, and then have the next car move and loop around to the beginning of the queue. How can I fix this problem?

Below is the script for the car nodes if it is relevant.

extends KinematicBody2D

onready var ray = $RayCast2D
var grid_size = 25

### MOVEMENT VECTORS OF CARS ###

var inputs = {
	'ui_up': Vector2.UP + 2*Vector2.RIGHT,
	'ui_down': Vector2.DOWN + 2*Vector2.LEFT,
	'ui_left': 2*Vector2.LEFT + Vector2.UP,
	'ui_right': 2*Vector2.RIGHT + Vector2.DOWN
}

### SIGNALS ###

signal move_completed

### SET VARIABLES FOR SPRITE TEXTURE CHANGES ###

onready var mysprite = get_node("Red Sedan")
var carTestNW = preload("res://Assets/Test Set/carTestNW.png")
var carTestSE = preload("res://Assets/Test Set/carTestSE.png")
var carTestSW = preload("res://Assets/Test Set/carTestSW.png")
var carTest = preload("res://Assets/Test Set/carTest.png")

### SET VARIABLES ###

func _ready():
	return

func _unhandled_input(event):
	for dir in inputs.keys():
		if event.is_action_pressed(dir):
			move(dir)
			emit_signal("move_completed")

func move(dir):
	var vector_pos = inputs[dir] * grid_size
	ray.cast_to = vector_pos # update position
	ray.force_raycast_update() # raycast looks where we want
	if !ray.is_colliding(): # ! = is not, if no collision, move
		position += vector_pos
		if Input.is_action_pressed("ui_up"):
			mysprite.set_texture(carTestSE)
		if Input.is_action_pressed("ui_down"):
			mysprite.set_texture(carTestSW)
		if Input.is_action_pressed("ui_left"):
			mysprite.set_texture(carTestNW)
		if Input.is_action_pressed("ui_right"):
			mysprite.set_texture(carTest)

I am very new to Godot, so I am sorry if anything is not understandable. Thank you for the help!

:bust_in_silhouette: Reply From: Black Monkey

You could try to change

var active_character

func initialize():
 active_character = get_child(0)

to :

 onready var active_character= get_child(0)  

Thank you for the quick response! Now the error is gone, but the cars are still not moving in turns, but rather at the same time. Do you know how I can see the value that “active_character” is returning in real-time?

turbostack | 2020-08-13 21:33