|
|
|
|
Reply From: |
Andrea |
As shown in your own example, once you write down a function, and attach the script to a node, from now on that node has this new method that can be called from any other script with
node.method()
The tricky part is only how to access said node.
If the calling script is the parent of the called node, you can use $
$NameOfTheChildNode.method()
Otherwise you have to state the path to reach that note by using
var node=get_node(path)
or get_child(x)
or get_parent()
, and probably other ways.
This is of course valid for the built-in methods and properties as well
Yes, but what about call
sirAlexDev | 2021-01-09 12:53
call is just the method used to call other methods, not very usefull tbh, you can call the method directly.
node.call("method")
is identical to node.method()
call_deferred()
is instead more useful, as it will call a method only during idle time (meaning it will wait till “normal” code execution will be finished, and then call the requested method)
i’m not 100% sure why the docs tells you to call the method do
using this complex form, instead of simply calling $Child.do()
.
The use i can think of is controlling which method to trigger by the use of a string, eg:
in the child node:
var method_name=""
func _process(delta):
if method_name!="":
call(method_name)
func Test1():
print("hello")
func Test2():
print("world")
in the parent node:
var x
x="Test1"
$Child.method_name=x
with this kind of structure you can control which method will be called during the process of the child, by changing the value of the string x.
In this case it will print “hello” at every process frame.
Andrea | 2021-01-09 13:59
This way of using call() lets you create your own rpc system. You send the name of the method and its arguments over a PacketPeer connection, and then on the receiving side call() the method.
func _process(delta):
while(myudp.get_available_packet_count() > 0):
var packet = myudp.get_packet()
packet = packet.get_string_from_utf8()
var called = packet.split(";")
call(called[0], called[1], called[2])
dkenshu | 2021-01-11 13:37
interesting concept!
Andrea | 2021-01-11 13:56
Thank you all!
sirAlexDev | 2021-01-14 13:50