You should have a look at how signals work: http://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_basics.html#signals
Emitting a signal from Primary.gd
and receiving it in Secondary.gd
can be done in the following way: first, you would have your two scripts written like this:
Primary.gd
extends Node
signal the_signal
# Then whatever code...
Secondary.gd
extends Node
# Note: inheriting Primary is not necessary for the signal to be received,
# so for this example I will not inherit it
func on_state_changed():
print("Update!")
Then you would have each of these scripts in two nodes, anywhere in your scene.
Now, for Secondary
to receive the signal, it has to be connected with Primary
.
You can do this from the scene tree editor: select the Primary
node, then click on the Node
dock. This dock should show all signals your node can emit, and you should see the_signal
in them. Double-click on it, a dialog will open asking you which node should receive the signal. Select Secondary
, and write the name of the function to call ("Method in Node"): on_state_changed
, validate and they should be connected.
There is another way to do this connection, with code, using the _ready
function. For example, in Secondary.gd
, you can do this:
func _ready():
var primary = get_node("relative/path/to/Primary")
primary.connect("the_signal", self, "on_state_changed")
or, if you prefer doing it from Primary.gd
:
func _ready():
var secondary = get_node("relative/path/to/Secondary")
connect("the_signal", secondary , "on_state_changed")
Finally, for this signal to do something, you have to emit it.
A simple way to test if it works is to emit the signal when you click a mouse button. To test this, you can use the _input
function in Primary.gd
:
func _input(event):
if event is InputEventMouseButton and event.pressed:
emit_signal("the_signal")