How to get the node that called the function?

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

There is a way?

:bust_in_silhouette: Reply From: Zylann

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:


Second node:

func the_function(caller):
	print("Called by ", caller)
:bust_in_silhouette: Reply From: AsP

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():

func foo():

func bar():

would print

[{function:bar, line:12, source:res://}, {function:foo, line:9, source:res://}, {function:_ready, line:6, source:res://}]