Hey guys, I am wondering if there is a way to use the value/contents of a variable in code as a function call or something similar. Essentially using a variable as another variable. For example lets say I had the following code (using standard name space):
1 2 3 4
|
int iLength;
int iSelect = 1;
string sString1 = "hello";
string sString2 = "hi";
|
and I wanted to find out the length of string 1. Traditionally I would just do:
|
iLength = sString1.length()
|
and get 5. However, is there a way I could use the value of the "iSelect" variable in my code to do the same thing? Something like:
|
iLength = sString(iSelect).length();
|
I just used parenthesis as an example (im not sure what the syntax would be) but the point is that the program would read that line as "iLength = sString1.length();" getting the "1" from iSelect.
I need to be able to do this because I am making a game in which there is a lot of logic that changes based on the room you are in, and this would really help clean up my code because the only way I know how to do it now is have an if statement for every possible room and then the code copy and pasted for each statement. I have about 40 rooms so as you can imagine if I want something to happen in a room that takes 10 lines of code that would be 400 lines instead of 10 just because I need to copy the code and use an if statement for each room.
I will explain a bit more if it helps get the point across, but if you understand already just skip this. Each room has a string associated with it that holds the items in the room. For example I have a room called "10" (so if your in room 10 the iLocation variable equals 10) and then a string called s10, and s10 = "ball bat glove bar". So if I wanted to do any manipulation of the items in that room ( or any room) I'd have to do something like s10.append or s10.erase and then have that same code 40 times (i.e. s11.append , s12.append) for each room under an if statement (i.e. if (iLocation == 10) ...) so that the program manipulates the correct string. It would be MUCH easier if I just had one iteration of my code that could be something like s(iLocation).append so that it would work for EVERY room because it would perform the action on the room items string based on what room you are in (the iLocation variable).
As another example, lets say the player wanted to drop the item "bag" in the room he was in. The only way I know how to do it now would be something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
int iLocation;
string sItem;
string s10 = "ball bat glove bar";
string s11 = "// ... so on so forth for each room;
cin << sItem; //user would input "bag"
FunctionDrop(sItem);
void FunctionDrop(Item)
{
if (iLocation == 10)
{
s10.append " ";
s10.append Item;
}
if (iLocation == 11)
{
s11.append " ";
s11.append Item;
}
//... for all 40 rooms
}
|
as you can see that would be alot of code. But if I can do what I'm talking about it would be reduced to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int iLocation;
string sItem;
string s10 = "ball bat glove bar";
string s11 = "// ... so on so forth for each room;
cin << sItem; //user would input "bag"'
FunctionDrop(sItem);
void FunctionDrop(Item)
{
s(iLocation).append " ";
s(iLocation).append Item;
}
|
and that would work for all 40 rooms.
I am really hoping something like this is possible because the code efficiency is sooooo much better than an if statement for each room. Sorry if this is confusing, ask anything that is needed.
Thank you.