Chaning variables with variable input ?

BlitzMax Forums/BlitzMax Programming/Chaning variables with variable input ?

Paul "Taiphoz"(Posted 2011) [#1]
I was wondering if its possible to dynamically do something like this.

global MyVar:int = 0

input "enter summit" > (user inputs - MyVar = 3)

and then have it actually change the value of that variable to 3 ?

I can fairly easily build a list of acceptable commands or variables to allow to change but want to know if its possible to do dynamic. so that I dont need to hard code a list of variables.


H&K(Posted 2011) [#2]
Pointers?
http://www.blitzbasic.com/bmdocs/command.php?name=Ptr&ref=2d_a-z
If I understand your question, then you pass the pointer of the variable to your "Input" function, (Rather than by reference), then your function can have access to that variable

Ps, Unless you are asking can the user input "Myvar=3" and the program lets myvar = 3

Last edited 2011


Paul "Taiphoz"(Posted 2011) [#3]
yeah the latter..

I'm going to write a Console routine that I will add to all my work, as a debug tool, so like slide the console window down and then type what I like, change lives, health, give more ammo etc.

Now I can of course just set up a lookup table of variables that I want to be able to change but that means actually having to code them in, so I am wondering id there is a more Dynamic way of doing it so if I add a new variable to the code for say controlling somethings speed, and then build the code I can instantly drop the console and change the variable speeds value, without having to add that variable to the list of variables I am allowed to change.

god this all sounds way more complex than it really is.

I know I could hack the running memory and write directly to the address of the variable but that will require creating a second tool to run at the same time, or i could just download a memory hacker from the net, but if at all possible I would rather have it built into the code itself.

I suspect its not possible but though I would ask anyway.


H&K(Posted 2011) [#4]
I suppose the first time you initiate each variable, you could add their pointer to a map, with the "key" being there name.

Then (theorising), you could simply search through the map for the right key, and then change the value. As long as the map and the variables never went out of scope, it should work. And you would only have to worry about it at variable initialisation and when you did the console thing.


Zeke(Posted 2011) [#5]
or use reflection:



Paul "Taiphoz"(Posted 2011) [#6]
that looks perfect, was looking for documentation on those commands but failed to find any, are my docs out of date or they just not documented well.

and thanks.


Zeke(Posted 2011) [#7]
Help->Modules->BASIC->Reflection


Yasha(Posted 2011) [#8]
I'm going to write a Console routine that I will add to all my work, as a debug tool, so like slide the console window down and then type what I like, change lives, health, give more ammo etc.


Perhaps a slightly more appropriate way of doing this would be to use a scripting engine for the console?


H&K(Posted 2011) [#9]
I didnt want to suggestion that (edit: Reflection) because you now are having to pass objects rather than variables, and need to re edit any code you have already done.

Whereas my suggestion only needs you to add the map code once, and the rest of any completed code need not change. And Ints (for example) stay as ints, rather than member variable ints. (In other langs Use namespace _G would make this criticism redundant)

(Thats not to say a global singleton object containing your Variables isnt a good idea, just that a lot of people dont do this, and your post implied you where one of them)

Last edited 2011


Paul "Taiphoz"(Posted 2011) [#10]
I normally actually take all globals and pull them into a type during my clean up and optimization phase, but with this console idea all I will do is start it from the get go so its there and available all the time.

I am thinking about(not tried it yet) spawning the console window as a thread so that I can interact with it while still having the rest of the game doing its thing, anyone think of a reason not to do it like that ?