how can i connect a signal to a newly instanced node
Asked Answered
A

4

1

i want to make it so that when i spawn an enemy, a signal is connected a to it. the signal has an intiger value. when the number attached reaches zero, a variable in the enemy script will change from true to false. heres how i handle spawning the node:

var enemy = preload("res://enemy.tscn")
var instance = enemy.instantiate()
add_child(instance)

the signal is setup like so:

signal amount(number)

func _process(delta):
emit_signal("amount", remaining_num)

any help would be great.

Araliaceous answered 3/12, 2023 at 17:25 Comment(0)
P
0

That will emit the signal 60 times per second. Do you really want to do that?

Pfeiffer answered 3/12, 2023 at 20:38 Comment(0)
A
0

Pfeiffer yea it needs to, since the number can change at any time. but im more worried about connecting the signal

Araliaceous answered 3/12, 2023 at 20:53 Comment(0)
P
0

Are you connecting the signal in code? That should be done by the enemy script in its _ready() method.

Which Godot version are you using?

Pfeiffer answered 3/12, 2023 at 22:3 Comment(0)
P
0

If you are using Godot 4.x

var enemy = preload("res://enemy.tscn")
var instance = enemy.instantiate()
instance.connect("_on_amount_changed", amount.bind(num))
add_child(instance)

func _on_amount_changed(amount):
print(amount)

Read more here Godot Signals

I will advise not to call emit_signal on every frame. You should use setters and getters to emit the signal of the amount changes.

Plenitude answered 3/12, 2023 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.