Page 1 of 1

Can i use strings content to affect variables in program?

Posted: Wed Dec 17, 2014 3:31 pm
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?

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

Posted: Wed Dec 17, 2014 7:12 pm
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

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

Posted: Wed Dec 17, 2014 8:54 pm
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.

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

Posted: Wed Dec 17, 2014 9:01 pm
by Allanon
That's wonderful to know! Thank you for the info :D

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

Posted: Wed Dec 17, 2014 9:23 pm
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.