#creating signals between scenes
48 messages · Page 1 of 1 (latest)
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
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
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
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)
hmm it just breaks if i set ship1 as an autoload tho
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
ah my bad, I'm fairly new to godot, so which exactly do i use as an autoload?
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
so do i create a new scene and script and have it autoload?
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
alright
yeah well the function is not needed
but yeah thats it
also connect without () at the end
connect(_on_ship_death)
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:
on level script
remove the () inside the connect
you want to use the function like a variable
not call it
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)
cool 🙂