Problem with my signal and for loop

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

Hi
In my project I’m spawning 3 Icons at the start of the game and put them in a Icon group
The Icons have each a MyNbr variable that goes from 0 to 2.
When I press the enter key, i execute this code:

if Input.is_action_just_pressed("ui_accept") and MyNbr == 0:
		emit_signal("Dead", MyNbr, position)
		self.queue_free()

The parent node receive the signal and launch this code:

func icon_moving(newMyNbr, newPosition):
	for Icon in get_tree().get_nodes_in_group("Icon"):
		if Icon.MyNbr == newMyNbr + 1:
			Icon.position = newPosition
			Icon.MyNbr = newMyNbr

What I’m trying to do is make the Icon with a MyNbr of 1 take the position and the MyNbr of deleted Icon.

The problem is that when i use Icon.MyNbr = newMyNbr, the icon that receives that newMyNbr is also deleted.

How do I fix this ?

Here is the full code for the parent

    extends Node2D

onready var Icontscn = preload("res://Sprite.tscn")
var ChildNbr = 0 
var SpawnPos = [Vector2(100,100), Vector2(100,200), Vector2(100,300)]

func _ready():
	for number in range(3):
		icon_spawn()

func icon_spawn():
	var IconInst = Icontscn.instance()
	add_child(IconInst)
	IconInst.connect("Dead", self, "icon_moving")
	IconInst.position = SpawnPos[ChildNbr]
	IconInst.MyNbr = ChildNbr
	ChildNbr += 1

func icon_moving(newMyNbr, newPosition):
	for Icon in get_tree().get_nodes_in_group("Icon"):
		if Icon.MyNbr == newMyNbr + 1:
			Icon.position = newPosition
			Icon.MyNbr = newMyNbr

And here the full code of the Icons

extends Sprite

signal Dead
var MyNbr = 0

func _ready():
	add_to_group("Icon")

func _process(delta):
	if Input.is_action_just_pressed("ui_accept") and MyNbr == 0:
		emit_signal("Dead", MyNbr, position)
		self.queue_free()
:bust_in_silhouette: Reply From: Inces

First look at this loop looks good to me.
Before I start thinking too much, I would like You to remove input check form process() to input(event). This is very propable, that process() resolves this input 2 or 3 times, even when it is JUST pressed. And this makes new icon take number 0 and immadietely die to next line of code.

It worked, Thank You !

Ph_Pheo | 2021-03-29 18:46