0 votes

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, "activecharacter" returns a value of null, and therefore the playturn 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!

in Engine by (21 points)

1 Answer

0 votes

You could try to change

var active_character

func initialize():
 active_character = get_child(0)

to :

 onready var active_character= get_child(0)  
by (52 points)

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?

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.