Convert string to Method/Function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Corruptinator
:warning: Old Version Published before Godot 3 was released.

Hello! after realizing that I can connect on an Area2D’s preset connections such as “body_enter” or “body_exit” through GDScript:

self.connect("body_enter",self,"_on_"+self.get_name()+"_body_enter")

I ran into a bit of a dead end when I noticed that using get_name() from the object generates the string needed for the connection, but is there a way to convert string into a method to allow me to call upon the scripted connection?

Example(Pseudo Code):

var conversion = ("body_enter",self,"_on_"+self.get_name()+"_body_enter")
self.connect("body_enter",self,"_on_"+self.get_name()+"_body_enter")

string to method(conversion):
 |
 V
func _on_[Name]_body_enter()

So if anybody knows this solution, then I would appreciate it. Thanks! : )

What’s the point of it? Why don’t you just use _on_body_enter as the method name?

vnen | 2017-01-22 14:39

I could do that, the problem though is say for example there are two nodes using the same script, and that if they both connect to the same than it could be a problem. Therefore, giving the connection a separated generated name helps keep things not only organized but separate in order to activate different triggers rather than activate all of them simultaneously through the same function name in a script.

Considering however, that may not be the case and I might not work, I’ll figure something out. Just curious to ask that question to see if it was possible. Thanks for replying though!

Corruptinator | 2017-01-22 14:43

:bust_in_silhouette: Reply From: Aquiles

Hi Corruptinator
Mmm, I do not understand very well, maybe you know but the logic of connect is (at least what I understand I understand and it worked, If I’m wrong, correct me.)

Connect (“Connection type” , where the function is located , function name , parameters or arguments of the function)

If many Nodes or objects use the same function, I recommend that the function solve what to do and not the connect

Example

node.connect("body_enter", self, "_my_function")

func _my_function(body):
    if body.get_name() == "enemy":
        #do this
    elif body.get_name() == "player":
        #do this

The node that has the code examines who enters its area and executes an action by means of the function

:bust_in_silhouette: Reply From: YeOldeDM

You can create binds to pass to your body_enter function to identify who is being entered.
Here, we go through each child of our node (maybe they’re Area2D or whatever) and connects their body_enter signals to our node. We bind a reference to that child’s node to the connection. Our function then has extra arguments to pass the additional variables to.

func _ready():
	for node in get_children():
		node.connect('body_enter', self, '_on_child_body_enter', [node])


func _on_child_body_enter(body, child):
	if child == get_node('Head'):
		if 'damage' in body:
			hit_in_head(body)
	if child == get_node('Foot'):
		if 'damage' in body:
			hit_in_foot(body)

Note we don’t use names, but a reference to the node itself. Trying to identify nodes by name is typically a bad idea, unless you have a good reason to do so.

Yes, it is a bad idea to use the names of the nodes, considering that the example I used the names, there are many ways to get references from a node, Can also be done by calling the group to which it belongs, I think this solution is the most optimal

Aquiles | 2017-01-27 02:07

:bust_in_silhouette: Reply From: lxknvlk

I have been able to do it like this:

    var callback = "method_name"
    if caller.has_method(callback):
        caller.call(callback, param1, param2, ...)