0 votes

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.

Thanks!

Godot version 3.4
in Engine by (362 points)

2 Answers

+1 vote
Best answer

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.

by (48 points)
selected by
0 votes

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

func _process():
    my_shared_function()

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

Alternatively:

var my_cool_variable 

func _ready():
    my_shared_function()

func _process():
    my_shared_function()

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 
by (3,870 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.