Connecting signal through several instances

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

In my game i have levels that are divided into waves. I have code to see if the current wave is finished, but i want to make it so that when it is it runs a function in the main to switch to the next wave using signals. I want the same code to run every time however, so how can i run the same function with signals from different nodes? Only 1 one is instanced at a time.

Can’t a signal from multiple nodes attach to the same function?

SIsilicon | 2018-04-28 01:25

:bust_in_silhouette: Reply From: funabab

If i understand your question correctly, you are trying to make all the objects that connect to your signal call a particular function.

one way to go about it is to pass the object​ that is emitting the signal along with the signal Eg. When you are calling the wave, you do something like this

# Wave Caller class
...
# new wave starting
    emit_signal("new_wave", self)
....
func wave_action(wave_object):
# this is the single function that all connected object will call

then when an object connects to it, in the callback function, you do something like this

func on_new_wave(wave_caller):
    wave_caller.wave_action(self)

Another way to also go about it is using FuncRef, but i don’t think that optimal in situations like this. I just hope this is what you are looking for

Thank you very much! I still have one issue however. The signal still doesn’t connect? I have this in main:

extends Node

var waves = ["res://Scenes/Levels/Waves/Wave 1.tscn","res://Scenes/Levels/Waves/Wave 2.tscn"]
var wavenum = 0
var currentwave = load(waves[wavenum]).instance()

func new_wave(wave):
	add_child(wave)
	wave.connect("new_wave",self,"_on_new_wave")

func _ready():
	new_wave(currentwave)

func _process(delta):
	pass

func _on_new_wave():
	print("wave over!")

and in the wave script i have this:

extends Node

export (bool) var finish = false
onready var children = get_children()
var flock
var blockmap

func _ready():
	add_to_group("wave")
	for i in children:
		if i.is_in_group("flock"):
			flock = i.get_name()
		if i.is_in_group("blockmap"):
			blockmap = i.get_name()
	print("hey the sequel")

func _process(delta):
	if get_node(flock).get_child_count() == 0 and finish == false:
		finish = true
		emit_signal("new_wave")
		for i in get_node(blockmap).get_children():
		    i.loop = false
	if get_child_count() == 0:
		queue_free()

lincolnpepper | 2018-04-30 16:19

Ok so i realized that the wave will always be a child of the main, so i just did get_parent and called the function from there. Thanks for the help anyway!

lincolnpepper | 2018-05-01 19:11