0 votes

i need to detect when all KinematicBody2D stop moving.
in KinematicBody2D script signal emitted when collision happened and yield used in root script with yield to wait for it to stop

KinematicBody2D script:

extends KinematicBody2D
var velocity

func _ready() -> void:
add_to_group("group1")

func _physics_process(delta) -> void:
    var collision: KinematicCollision2D = move_and_collide(velocity * delta)
    if collision:
        emit_signal("stopped")

func move(v) -> void:
    velocity = v * speed

root script

extends Node

func _input(event) -> void:
    if event.is_action_pressed('ui_right'):
        move_all(Vector2(1, 0))
    if event.is_action_pressed('ui_left'):
        move_all(Vector2(-1, 0))
    if event.is_action_pressed('ui_down'):
        move_all(Vector2(0, 1))
    if event.is_action_pressed('ui_up'):
        move_all(Vector2(0, -1))

func move_cells(direction):
    set_process_input(false)
    var x = get_tree().get_nodes_in_group("group1"):
    x[0].move()
    x[1].move()
    yield(x[0], "stopped")
    yield(x[1], "stopped")
    set_process_input(true)

it works as far as first body registered with yield emits signal first
but if the second body registered with yield emitted signal first yield doesn't end
what should i do to detect all in no specific order

in Engine by (15 points)

1 Answer

+1 vote
Best answer

Your implementation relies on the order that the objects start/stop moving. What I'd do for this would be:

  1. Add a signal that is emitted when each object starts moving ("started_moving")
  2. Start a counter in the listener object to keep track of how many objects are moving
  3. Connect this signal with a function that adds 1 to the counter of moving objects
  4. Connect the "stopped" signal to a function that subtracts 1 from the counter of moving objects, and if the number is 0 then all of your objects have stopped moving!
by (72 points)
selected by

Thanks for Your help

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 webmaster@godotengine.org with your username.