how to detect all KinematicBody2D in scene stopped moving

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

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:

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

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):
	var x = get_tree().get_nodes_in_group("group1"):
    yield(x[0], "stopped")
    yield(x[1], "stopped")

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

:bust_in_silhouette: Reply From: brazmogu

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!

Thanks for Your help

a7med | 2020-02-26 19:50