Can i use strings content to affect variables in program?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Can i use strings content to affect variables in program?

Post by Bugala »

I do have a way to do this, but it isnt very good way, as example:

Code: Select all

mytable = {
          ["variable"] = 1
                }

mystring="variable"

mytable[mystring] = 2
I can do it this way, but what i would like to do, is to be like this:

Code: Select all

variable = 1
mystring = "variable"
mystring = 2
and variable value changes into 2.


What I am after with this is that perhaps I would like to debug my program, and it could be handy that i could in middle of program push some predefined button, as example "c", and it would ask me to give me both variables name, and its new value. Then i would simply put for example: "Speed" and "10", and after this, program would execute "Speed = 10", and i could see if bug occurs now or not.


The beginning version is one possiblity, but it means that all my variables should then reside in one and same table. What I would like to have, is to still have the freedom of using my own variables like i have been doing before, yet be able to change their values using Strings to tell what variable i want to be changed.

Is this possible, and how?
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Can i use strings content to affect variables in program

Post by Allanon »

It's some time I'm thinking how to implement an interactive mode like the console mode we see in some games, unfortunatly actually my free time is almost zero :P
Anyway, unless Andreas have a custom command like the one used for the constants (GetConstant) the only way is to store all variables in a table. I usually do this for my app and libraries.
For the app I use a table named "app" for all variables and tables.

@Andreas
It could be difficult to implement a couple of commands like:
value = GetVariable(variable_name)
SetVariable(variable_name, value)
:D
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
User avatar
airsoftsoftwair
Posts: 5887
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Can i use strings content to affect variables in program

Post by airsoftsoftwair »

Actually, it's already possible by peeking/poking into an invisible table that goes by the name of "_G". All globals are stored in this table, so you could just do something like this:

Code: Select all

Function p_SetVar(name$, v)
	RawSet(_G, name$, v)
EndFunction

Function p_GetVar(name$)
	Return(RawGet(_G, name$))
EndFunction

testvar = 6
p_SetVar("testvar", 7)
DebugPrint(p_GetVar("testvar"), testvar)
Output is:

Code: Select all

7 7
Be sure to use only variable names in lower case. It won't work with mixed case or upper case variable names.
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Can i use strings content to affect variables in program

Post by Allanon »

That's wonderful to know! Thank you for the info :D
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can i use strings content to affect variables in program

Post by Bugala »

I was actually myself thinking that it might be possible to do somehow through that global list, just couldnt figure out myself how to do it.

Thanks a lot! This is very helpful option.
Post Reply