GDScript: Can I get the name of a function from within the function?

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

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

var ME

signal DebugPrintLine(from, txt)
signal DebugPrintNewLine(from)

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 get_method_list() 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.

:bust_in_silhouette: Reply From: Ninfur

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.


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


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

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.

chimeforest | 2022-11-02 21:43

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

Ninfur | 2022-11-02 21:51

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

chimeforest | 2022-11-03 22:10