+2 votes

There is a way?

in Engine by (692 points)

2 Answers

+2 votes
Best answer

You can't get the caller of a function, there is no point doing that. If you really need to, just pass it as an argument, like so:

First node:

node.the_function(self)

Second node:

func the_function(caller):
    print("Called by ", caller)
by (29,090 points)
selected by
+2 votes

If you don't actually need the node, but only a trace to the caller, you can also use get_stack() to get an array of the whole stack, index 0 being the innermost function. From the docs:

Array get_stack( )

Returns an array of dictionaries representing the current call stack.

func _ready():
    foo()

func foo():
    bar()

func bar():
    print(get_stack())

would print

[{function:bar, line:12, source:res://script.gd}, {function:foo, line:9, source:res://script.gd}, {function:_ready, line:6, source:res://script.gd}]
by (34 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.