0 votes

Hello! after realizing that I can connect on an Area2D's preset connections such as "bodyenter" or "bodyexit" through GDScript:


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

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

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

in Engine by (206 points)
edited by

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

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!

3 Answers

0 votes

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


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

by (84 points)
0 votes

You can create binds to pass to your bodyenter 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:
    if child == get_node('Foot'):
        if 'damage' in 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.

by (1,328 points)

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

+3 votes

I have been able to do it like this:

    var callback = "method_name"
    if caller.has_method(callback):
        caller.call(callback, param1, param2, ...)
by (74 points)
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.