Jump to content
  • Advertisement
Sign in to follow this  
rastmark

Setting variable value by the name

This topic is 3669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a variable v1 in my program. Now I want to type in the variable name and value from the command prompt, and save the value for the variable given. Like this: >SET v1 4 And the program would this: v1=4; Basically I have to do now this now: if(variableNameGiven == "v1")then v1=givenVariableValue; But is it possible, somehow, to do it like this without any if's and stuff: variableNameGiven.value = givenVariableValue; Got it?

Share this post


Link to post
Share on other sites
Advertisement
You can do that quite easily in SH, PHP, LISP and ECMAScript, and possibly also in Python and Perl. You can do that with a small effort in .NET and Java through reflection. In C and C++, you have to do the name-variable correspondence manually.

Share this post


Link to post
Share on other sites
The user writes "v1" and "lskjdf". You have to determine that "v1" is a variable (and what it corresponds to) but that "lskjdf" isn't a variable. In order to do this, you have to somehow have a list of variable names in your program.

The compiler deletes all variable names when it creates the program (although it will sometimes keep some of them in an implementation-dependent, configuration-dependent, hard-to-read-and-understand way). The closest you can get is creating a DLL which exports some functions that can be queried by name.

So, you're much better off writing these bindings by hand instead, or using an embedded Python interpreter, or using a script language instead of C++ for whatever you're doing (command-line PHP works pretty well and is fairly close to C++ in terms of syntax).

Share this post


Link to post
Share on other sites
Not automatically. But there is almost certainly a way to do whatever it is you'd like to do. What is it you're trying to accomplish? An in-game console? A configuration file?

Share this post


Link to post
Share on other sites
You should just be using a simple lookup table like std::map. Have the key be a string (the variable name) and the value be whatever datatype you're using. From here you can lookup and create new values easily on the fly.

Share this post


Link to post
Share on other sites
How about having single character as the variable name in console?


int v1, v2, v7, v11;
char v5, v6, v10;

void *array[256];
array['1'] = &v1;
array['2'] = &v2;
array['3'] = NULL; // ( I am not sure if you have to insert NULL lines or if compiler will do it for you. )
array['4'] = NULL;
array['5'] = &v5;
array['6'] = &v6;
array['7'] = &v7;
array['8'] = NULL;
array['9'] = NULL;
array['A'] = &v10;
array['B'] = &v11;

...

char symbol = user input;
some_variable value = user input;

if(!(symbol >= '1' && symbol <= '9') && !(symbol >= 'A' && symbol <= 'B')){
// Invalid name
}
else{
void *a = array[symbol];
if(a != NULL){
*a = value;
}
else{
// Invalid name again
}
}

Share this post


Link to post
Share on other sites
You can use the preprocessor to create a table mapping the string name of the variable to the address of a variable. Then you just do a lookup in that and set the value.

TABLE_START(MyMappingTable, int)
TABLE_ENTRY(v1)
TABLE_ENTRY(v2)
TABLE_ENTRY(FooBar)
TABLE_END

Then you need to write the macros TABLE_START, TABLE_ENTRY, and TABLE_END. TABLE_ENTRY must stringify the variable name as well as take the address of the variable by that name, and each table entry will contain a const char*, plus say an int*. I believe the TABLE_ENTRY marco will contain the # and/or ## operator(s) which is not something I use very often. Someone will show you how to do that.
Of course this assumes that your variables are all of the same type, e.g. int, but you can name them whatever you like. There are ways around the one-type problem as well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!