Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | turbostack |
Hello, I am trying to make a simple turn-based movement demo. The structure of my code is as such:
Node2D
- Kinematic Body 1
- Kinematic Body 2
- Kinematic Body 3
The script on Node2D is supposed to manage the turns of each kinematic body. Each kinematic body should only move once, and then the turn must transfer to the next kinematic body.
I have it configured such that the parent script waits for a signal “completed” from the child node, and then makes the next node in the tree the “active character” playing a turn. Below is the parent script.
extends Node2D
class_name TurnQueue
var active_character = get_child(0)
func play_turn():
yield(active_character, "completed")
var new_index : int = (active_character.get_index() + 1) % get_child_count()
active_character = get_child(new_index)
Below is the script being used by the parent node’s children.
extends KinematicBody2D
signal completed
var inputs = {
'ui_up': Vector2.UP,
'ui_down': Vector2.DOWN,
'ui_left': Vector2.LEFT,
'ui_right': Vector2.RIGHT
}
func _unhandled_input(event):
for dir in inputs.keys():
if event.is_action_pressed(dir):
move(dir)
emit_signal("completed")
func move(dir):
position += inputs[dir] * 500
The script for all the children each have a signal connected to the play_turn()
function in the parent script. What is currently happening is that all three kinematic bodies will move together once, then I get the two errors that stop the game:
First argument of yield() is null.
and
get_child: Index p_index = 0 is out of bounds (data.children.size() = 0).
I am very new to Godot, please let me know if anything is unclear. Your help is appreciated!