+1 vote

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. enter image description here 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.

Godot version 3.2.3
in Engine by (36 points)

1 Answer

+1 vote

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

by (1,514 points)

Yes, but what about call

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.

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])

interesting concept!

Thank you all!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.