Simple turn-based movement demo

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

Hello, I am trying to create a simple turn-based movement demo. Currently, there are three separate sprites that move across the screen a predefined distance upon the right arrow key press. I want them to each take turns moving. One press of the right arrow key should be one movement, and then it should allow the next Sprite to move, then the next sprite after that.

Originally, I used signals to move the first, then second, third, and lastly fourth sprite. But I could not find a way to loop back around in the queue and start again. They would all begin moving again together afterwards. I’ve tried following GDQuest’s turn-based movement demo, but having trouble putting it all together.

The structure looks like this:

Node2D

  • Sprite 1
  • Sprite 2
  • Sprite 3
  • Sprite 4

All of my sprites all use the following code:

extends Sprite

signal end_turn

var velocity = Vector2(0, 0)

var speed = 200

func _process(delta):
	if Input.is_action_just_pressed("ui_right"):
		velocity.x = 50
		global_position += velocity * speed * delta
		emit_signal("end_turn")

I’m trying to manage the turns by using the following script on the parent Node2D.

extends Node2D

class_name TurnQueue

onready var active_character

func initialize():
	active_character = get_child(0)

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

I connected the "end_turn" signal from each child to the parent play_turn() function already. What happens now is that the first sprite moves once, then I get the following error: “First argument of yield() not of type object.”

Is there a way to fix this? Or am I approaching it all wrong? Your help is much appreciated. I am still very new to Godot but I am happy to answer any other questions about the code.

:bust_in_silhouette: Reply From: njamster

Have you made sure that initialize()is called before play_turn() is called for the first time and that the node already has the correct 0th-child at this point in time? As long as active_character is set to any node this should work as each node in Godot is derived from Object! Try print(active_character) before the yield.