0 votes

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.

in Engine by (243 points)

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

1 Answer

0 votes

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

by (112 points)

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()

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!

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.