Page 1 of 1

Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 11:21 am
by diceman
I wonder if this is possible ... :)

I declared a variable:

Code: Select all

Global foo
Now I want to output the name of the variable as a string$, so when running the program, in the Debugger window it should print:

Code: Select all

foo
Thanks!

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 11:46 am
by AZJIO
Map?
In compiled languages, the name is not stored. But you can make a connection through "Map".

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 11:46 am
by Fred
I don't think it's possible right now, what you want it something like nameof() in C#

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 12:11 pm
by diceman
OK, thanks, thought so. :)
But interesting nonetheless, that C# can do that.
No biggie. Was just asking, because I'm currently coding the custom keyboardMapping routine in my game.
Gotta do this manually then with a "buttonID translation routine". :D

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 12:23 pm
by jacdelad
AZJIO wrote: Mon Nov 03, 2025 11:46 am Map?
In compiled languages, the name is not stored. But you can make a connection through "Map".
How does the dialog library connect the variable name in a string to the actual variable?

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 12:27 pm
by BarryG

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 12:29 pm
by TI-994A
diceman wrote: Mon Nov 03, 2025 12:11 pm...Gotta do this manually then with a "buttonID translation routine". :D
Just curious: what would the "button ID translation routine" do, exactly?

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 12:34 pm
by diceman
TI-994A wrote: Mon Nov 03, 2025 12:29 pmJust curious: what would the "button ID translation routine" do, exactly?
I can call the procedure with a String (e.g. "#PB_Keyboard_Return") and then get in return the respective buttonID (#PB_Keyboard_Return), and another procedure with a vice versa functionality (pass a variable, and get the corresponding String for display).

@Barry
The GetRuntimeInteger()-thing certainly looks interesting, thanks!
Maybe when I get to do this again sometime, for now I'm satisfied with my "dirty" solution. Wasn't really a problem to solve, just boring copy/paste and more "paperwork" stuff. Not very elegant, but gets the job done. 8)

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 12:56 pm
by mk-soft
Update ;)

Code: Select all


Macro _dq_
  "
EndMacro

Macro MyDebug(_value_, _proc_=#PB_Compiler_Procedure, _line_=#PB_Compiler_Line)
  Debug "Proc "+_proc_+" / Line "+_line_+": Variable "+_dq_#_value_ = _dq_ + _value_
EndMacro

Global foo = 100

MyDebug(foo)

Procedure MyFunction()
  Protected iValue = 1000
  MyDebug(iValue)
EndProcedure

MyFunction()

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 1:26 pm
by AZJIO
jacdelad wrote: Mon Nov 03, 2025 12:23 pm How does the dialog library connect the variable name in a string to the actual variable?
AutoIt3 has the Assing() and Eval() functions, one creates a variable from a string, the other reads a variable from a string. I once needed access to a list of variables, and I created it using these functions before I knew what an array was. You can store a name in a map and access its value. Just create an example, even if it doesn't work, and I'll convert it into an example using a map.

Code: Select all

name$ = "5"
debug nameof(name$)

AddMapElement(Map(), "name$")
Map() = "5"
debug Map()

Re: Getting name of a variable as a string$ during runtime?

Posted: Mon Nov 03, 2025 1:43 pm
by diceman
Interesting, thank you! :shock:

Re: Getting name of a variable as a string$ during runtime?

Posted: Sat Nov 08, 2025 9:54 pm
by Piero
mk-soft wrote: Mon Nov 03, 2025 12:56 pm Update ;)

Code: Select all

CompilerIf #PB_Compiler_Debugger
   Macro _dq_ : "
   EndMacro
   Macro MyDebug(_value_, assert=1, _proc_="PROC "+#PB_Compiler_Procedure+" / ", _line_="LINE "+#PB_Compiler_Line+" /")
      If assert
         Debug _proc_+_line_+" VAR "+_dq_#_value_ = _dq_ + _value_
      Else
         Debug "LINE "+#PB_Compiler_Line+": "+_dq_#assert#_dq_+" is not true!"
         CallDebugger ; "Breakpoint"
      EndIf
   EndMacro
CompilerElse
   Macro MyDebug(a,b=,c=,d=) : EndMacro
CompilerEndIf

Global foo = 101

MyDebug(foo,1,"")
MyDebug(foo, foo>100, "All OK; foo is greater than 100:","")

Procedure MyFunction()
   Protected iValue = 1000
   MyDebug(iValue)
EndProcedure

MyFunction()
;)