Any example of using call method

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

I want to figure out the best ways of setting up the scene tree and manipulation of the data between differant scenes. So i read a documentation article Scene organization. But I don’t understand how Call works. According to Docs I need to have a property MethodName in the child and the function with this name And set value of this property from parent and i can do Call(MethodName) only from this particular chuld!? I just can have a function at child and cal it from there without setting property. I hope I’m wrong and Call method is really pawerful and usefull. And maybe I can set the function in Parrent and call it in child, Or Call it not only in child.

:bust_in_silhouette: 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