0 votes

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()
Godot version 3.2
in Engine by (17 points)

1 Answer

0 votes
Best answer

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.

by (2,432 points)
selected by

It worked, Thank You !

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 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.