Way to get which function may have called another function?

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

Just curious if there is a way to check what function may have called another function. The Debugger appears to be able to chain together what functions called which. Is there a way to use this information via the script?

For example; I’d like to know if a function is being called via the _ready() or _process() without having to manually pass an argument that differentiates the two.


:bust_in_silhouette: Reply From: octopod

To my knowledge, there isn’t one. As you described passing an argument to the function that differentiates who called it is likely the best way to achieve what you want.

There’s a slight issue with wanting to use the debugger in your script in order to look at the stack (the chain of functions being called) and it’s that the debugger won’t exist in your release build. However, if you want to know what is calling your function for debugging reasons you can add breakpoints in that function in order to automatically stop the game and allow you to look at the stack.

:bust_in_silhouette: Reply From: timothybrentwood

get_stack() will get you all the information you need in regards to the call stack. This function was intended for debugging. A function should not rely on knowing who calls it to execute.

If you are wanting to do some setup when called from _ready() but not when called from _process() there are much better ways:

var my_member_variable 

func _ready():

func _process():
func setup_for_my_shared_function()
    my_member_variable = ["foo", "bar"]
func my_shared_function()
    var a = my_member_variable[0] + " now i'm doing the rest"
    var bar = my_member_variable[1]
    my_member_variable = ["bar", "baz"]


var my_cool_variable 

func _ready():

func _process():

func my_shared_function()
    if not my_cool_variable:
        my_cool_variable = "is now setup"
    var i = "now i'm doing the rest" + my_cool_variable