#creating signals between scenes

48 messages · Page 1 of 1 (latest)

silk lodge
#

Anyone know how to make it so i can receive a signal from one scene to another?

#

I'm trying to make it so that when ship1 collides with the meteor it sends out a signal to LevelScript

#

so that LevelScript can know when ship1 has been destroyed

bold sigil
#

In your first script you might want to emit the signal before calling queue_free

#

and in the second one, you can use a better syntax that will ensure the signal and your method exists : ship1.ship_death.connect(ship_death_test)

#

the analyser will check that the signal exists on the ship1 instance and that the method you connect it to exists too

silk lodge
#

alright so ive changed it up and when i run the game it says:

Invalid call. Nonexistent function 'find_node' in base 'Window'.

#

could be a problem here

bold sigil
#

yeah, that's not really the best way to do this. You probably want an event manager that will handle the signal so the two nodes don't have to reference each other

#

Basically, you would need an autoload (let's call it EventManager) that declares a signal ship_death(ship)
Then in your level script, do EventManager.ship_death.connect(_on_ship_death) with a method func _on_ship_death(ship)
and when your ship dies it just needs to call EventManager.ship_death.emit(self) and the level will be notified

#

(sorry, I'm going fast, but I've got to go)

silk lodge
#

hmm it just breaks if i set ship1 as an autoload tho

bold sigil
#

what ? no don't do that

#

but your level script should not need to reference the ship, it can just be passed as an argument of the signal callback

silk lodge
#

ah my bad, I'm fairly new to godot, so which exactly do i use as an autoload?

wise herald
#

create a new script

#

this is just used as an event bus to emit events to

#

and then since its autoloaded , any script can listen to the events from anywhere

#

by connecting to them using connect

silk lodge
#

so do i create a new scene and script and have it autoload?

wise herald
#

go to the autoload menu and select the .tscn file

#

and add it as an autoload

silk lodge
#

alright so ive set it up as this now with the autoloaded tscn

#

it prints this out

#

i havent wrote anything in the new script file as well

wise herald
#

you need to create the signal

#

signal ship_death(ship)

#

in the eventbus script

silk lodge
#

alright

wise herald
#

yeah well the function is not needed

#

but yeah thats it

#

also connect without () at the end

#

connect(_on_ship_death)

silk lodge
#

wait so should on_ship_death be on Eventbus or Levelscript?

#

Eventbus script looks like this:

#

ship1 script looks like this:

#

and Level script looks like this:

wise herald
#

on level script

#

remove the () inside the connect

#

you want to use the function like a variable

#

not call it

bold sigil
#

also _on_ship_death should take an argument

#

because the ship will send itself to it

#

and it should be signal ship_death(ship) (even better with types, but one topic at a time)

silk lodge
#

let's gooo i got it to work :))

#

thanks guys

bold sigil
#

cool 🙂