0 votes

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:

- 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

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.

in Engine by (21 points)

1 Answer

0 votes

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.

by (10,582 points)
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.