In a game I am working on, I have had a long chain of if/else if blocks to compare a command the player has entered with some strings representing valid commands. I then run the applicable function.
1 2 3 4
|
if(input == "com1") func1();
else if(input == "com2") func2();
else if(input == "com3") func3();
// and so on
|
I'm sure there is a better way to do this; especially since eventually MSVC++ is going to complain once I get over a certain number of nested statements. One idea I had was to load from a file that contained valid commands followed by the name of the function to run; I could store those as a vector of pairs or a map, etc. and then use a simple for loop to call a function pointer to the right function.
Example file:
com1 func1
com2 func2
com3 func3 |
The only issue is that I'm not certain how (or if) I can convert the string name of the function loaded from the file into the address of the function. If this isn't possible, does anyone have any ideas as to how I could accomplish the input string comparison without use the if/else if chain?
I was going try a method similar to my idea: hash the function names to an integer and use a switch/case, which would (I believe) avoid the nested if/else problem, but I was using the boost hashing system which doesn't create hashes for constants at compile-time, and couldn't be used in a case statement.
I apologize if I was unclear anywhere; ask for clarification if I didn't make any sense somewhere. Any input for my problem is welcome at the moment.