What's the code of getting a line number from a script? Similar to __LINE__ from C++.

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

I want to know what’s the code of getting a line number from a script. My point is I’m looking of a gdscript function similar to LINE from C++.

:bust_in_silhouette: Reply From: r.bailey

So there might be other ways, but one way I know is to get the stack and pull the data from that. You could do something like I have done below, I forget how the stack builds, as in when you call it in nested functions and all. But I am pretty sure you can make it work either way. The function below is just an example of building it all out of course.

func debugTest(optionaltext:String) -> void:
var frame = get_stack()
var line = "Line:" + str(frame[0]["line"])
var source = "Source:" + frame[0]["source"]
var function = "Function:" + frame[0]["function"]
print("%s - %s - %s - %s" % [source,function,line,optionaltext])

Hope it helps.

Note that the stack is only available in debug builds (editor/projects exported in debug mode), not in projects exported in release mode.

Calinou | 2021-09-22 00:59

Thanks, good to know. I assume it is for debugging anyways so hopefully that will be good enough for his/her purposes.

r.bailey | 2021-09-22 15:41