How can you get access to a function of a nother script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By G2700

How can you get access to a function of a nother script

:bust_in_silhouette: Reply From: purple_mage

with signals

you .emit() a signal from one script then you .connect() with another script

:bust_in_silhouette: Reply From: godot_dev_

In addition to using signals, as suggested by @purple_mage, you could also have the function of a child node’s script be accessed by a parent node’s script. In the _ready function of your parent node script, you could do something like childNodeRef=$MyChildNode, where childNodeRef is a member of the parent script and MyChildNode is the name of the child node. Then in your parent script, if you want to access a function called sayHello of the child node, the parent would simply do childNodeRef.sayHello().

Now if you want a child of the parent to call a function of another child of the parent, you could have the parent node’s script give a reference of the children to each children. For example, say your child nodes are named Child1 and Child2, respectively. Now suppose both chilren have a member variable named myPeer. Then the parent, in the _ready function could setup myPeer as follows:

var child1 = $"Child1"
var child2 = $"Child2"
child1.myPeer = child2
child2.myPeer = child1

Now the children can access each others functions. For example, we can have Child1 call Child2’s sayHello function by having the Child1 script use myPeer.sayHello()