+1 vote

I'm writing a debug console for a project. I want it to know who is sending the debug messages to it can decide which ones to display or not based off of an ignore list. That way I don't have to comment out all my debug messages for X when I start working on Y, I can just tell the debug console to ignore X.

Since Godot doesn't have any code tracing, I've been sending that info as a parameter. Which is fine, except I have to alter the code for every function because I don't know if there's a way for a function to know it's own name.

Here is the barebones what I have so far:

extends Control

#VARS--------------------------
var ME

#SIGNALS-----------------------
signal DebugPrintLine(from, txt)
signal DebugPrintNewLine(from)

#Functions---------------------
func _ready():
    #assign ME to current script path
    ME = get_script().get_path().right("res://Scripts/".length())

func _on_LoadFileDialog_file_selected(path):
    #assign ME_text to ME + func name
    var ME_txt = ME + "/" + "_on_LoadFileDialog_file_selected()"

    #send debug messages
    emit_signal("DebugPrintNewLine", ME_txt)
    emit_signal("DebugPrintLine", ME_txt, "Opening: " + path)

    #do other stuff

If I could change

    var ME_txt = ME + "/" + "_on_LoadFileDialog_file_selected()"

to something like

    var ME_txt = ME + func_name()

That would make the scalability of this workaround that much easier.. I could just copy that line to the top of every function instead of having to tweak it each time.

I've looked into getmethodlist() and I can narrow it down to (mostly)my functions... but that's about it since I don't know how to identify the exact function I want without tweaking the code each time.

I've also thought about reading the gd file as text and getting function names that way.. but it's still the same problem.

Any suggestions would be appreciated!
Thanks for reading all this =]

EDIT: If possible, I need a solution which will work during runtime, not just in debug mode.

Godot version v3.5.1.stable.official.6fed1ffa3
in Engine by (24 points)
edited by

1 Answer

0 votes

It is possible to get the current function's name from the call stack. However, be aware that this will only work in debug mode.

get_stack()[0]["function"]

The call stack also contains the current line number and script path.

get_stack()[0]["line"]
get_stack()[0]["source"]

There are also the built in functions print_debug(...) and print_stack() that will print this information for you.

by (1,083 points)
edited by

Thank you, this is very useful information!

Unfortunately, I need an option which will also work at runtime if possible.
I'll edit my question to reflect that.

As far as I am aware that is not possible, but maybe someone got a more definitive answer.

I was afraid that might be the answer, but I figured I'd ask anyway just in case.
Thanks for your help =]

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.