Is it possible for a button in one scene to trigger a method in a different scene? If so, how would I do that? The reason for this is because when I press the button in the 1st scene, I want it to spawn a sprite in a 2nd scene.
The button’s signal is shown in the node “1” below:
you are connecting the returned value of your method to your signal.
Use your method name , without the parentheses.
Try:
func _ready():
SignalBus.cargo_sent.connect(airship_transit) # use SignalBus, and remove the parentheses of airship_transit.
Also, in your signal_bus.gd, you use the class_name SignalBus, so use SignalBus, the class name, to access anything inside.
Also, your are emitting a signal in SignalBus, so you don’t need a signal in you “Loading_cargo” script. So
extends TileMapLayer
# signal cargo_sent() This line can be removed
func _on_send_cargo_pressed():
SignalBus.cargo_sent.emit() # use SignalBus, instead of signal_bus
Also, make sure you signal_bus.gd script is auto loaded, so it can be access everywhere. Doc is here: Singletons (Autoload).
# signal_bus.gd
class_name SignalBus
extends Node
signal cargo_sent()
I do have the signal_bus autoloaded:
Despite all this, I am getting the "Cannot find member “cargo_sent” in base “SignalBus” error. I am unsure why “cargo_sent” is not recognized, despite having it in the signal_bus script.
Once you autoload a script, you don’t need a class_name. You can use the name when you set the autoload scrip. In your case, “signal_bus”.
The class_name SignalBus is not needed in a autoload script.
# signal_bus.gd
# class_name SignalBus #This line should be removed
extends Node
signal cargo_sent()
And when you need to access your autoload script, use “signal_bus”. This is the name you fill into the Node Name section when you add your script .
I suggest you can just change you name from “signal_bus” to “SignalBus” in the autoload setting. That should fix everything, and you can continue using SignalBus to access your script everywhere.
If you still want to use the name “signal_bus” in autoload setting , then use signal_bus in you code:
func _ready():
populate_cities()
signal_bus.cargo_sent.connect(airship_transit) # signal_bus instead of SignalBus
func _on_send_cargo_pressed():
signal_bus.cargo_sent.emit() # signal_bus instead of SignalBus
I misremembered how autoload work, class_name is not needed, sorry about that.
No worries, I appreciate your feedback. That fix did allow the game to run now, so it worked out. However, for the airship_transit method, I have the following code so far:
func airship_transit():
print("1")
When I test the signal, I don’t see the “1” in the output section of Godot. The signal originates from the following:
The ‘income_made.text’ does get updated on the screen, so I know the ‘_on_send_cargo_pressed()’ method is working as intended. The signal should be sent. But I don’t see the ‘airship_transit’ output for some reason.
Check if airship_transit method lives in a script that binds to an existing node on the scene tree when you press the send_cargo button.
Check order of execution. Dose the _ready function connecting the signal called after the signal is sent. You can add a print function in _ready to check.
Did some debugging and as it turns out, the _ready function connecting the signal does not get triggered once the “_on_send_cargo_pressed()” is triggered. Why is the _ready() method not being activated and how do I fix this?
FYI, the “airship_transit()” method is on the ‘TileMapLayer’ script under the World_Map scene. The “_on_send_cargo_pressed()” is in a script for a tilemaplayer named “Cargo” in the 'loading_cargo" scene.
The only reason I can possibly imagine is that you create the node, whose script containing airship_transit(), after send_cargo button press.
_ready function triggers when a node enters scene tree. If your node is created at runtime(not already in the scene tree before you run your game, usually by instantiate a PackedScene), then the _ready function of this node will only be called after you add it to the scene tree with ParentNode.add_child. Check the doc: _ready vs. _enter_tree vs. NOTIFICATION_PARENTED.
So, if you are creating you node on the fly, then _ready happens after add_child.