J
Jim Langston
In my program I am accepting messages over the network and parsing them. I
find that the function that does this has gotten quite big, and so want to
break the if else code into functions. I started thinking of how to do
this, but came up with a number of ways and don't know what would be best,
and fit into the C++ idoism.
This is what I have now:
if ( ThisPlayer.Character.GMLevel == 100 && ( StrMessage == "/debugserver"
|| StrMessage == "/DEBUGSERVER" ) )
DebugMode = ! DebugMode;
else if ( StrMessage == "/list" || StrMessage == "/LIST" )
{
// lots of cod here
}
else if ( ThisPlayer.Character.GMLevel >= 50 && ( StrMessage.compare( 0, 8,
"/summon " ) == 0 || StrMessage.compare( 0, 8, "/SUMMON " ) == 0 ) )
{
// lots of code here
}
else if ( ThisPlayer.Character.GMLevel >= 50 && ( StrMessage.compare( 0, 8,
"/jumpto " ) == 0 || StrMessage.compare( 0, 8, "/JUMPTO " ) == 0 ) )
{
// lots of code here
}
etc.. There are actually 30 different else if's. As you can imagine, the
code is quite huge. The simplest thing would to take each // lots of code
here and make them functions and call them. But then I was thinking I could
just pass the GMLevel and StrMessage to the functions themselves. Then I
started thinking about maybe having some type of custom map type container
with the criteria (keyword and GMLevel) and a pointer to the function,
etc...
Over time this is just going to grow. This is the main text processing for
my online game and commands will only get added, not removed over time.
I can think of having a structure/class with Minimim GMLevel, keyword,
pointer to function call. There shouldn't be too much of a problem with
having to pass parameters to the class because of the game system constrains
I have a global structure that I can access what I need anyway.
What would be your suggestion?
find that the function that does this has gotten quite big, and so want to
break the if else code into functions. I started thinking of how to do
this, but came up with a number of ways and don't know what would be best,
and fit into the C++ idoism.
This is what I have now:
if ( ThisPlayer.Character.GMLevel == 100 && ( StrMessage == "/debugserver"
|| StrMessage == "/DEBUGSERVER" ) )
DebugMode = ! DebugMode;
else if ( StrMessage == "/list" || StrMessage == "/LIST" )
{
// lots of cod here
}
else if ( ThisPlayer.Character.GMLevel >= 50 && ( StrMessage.compare( 0, 8,
"/summon " ) == 0 || StrMessage.compare( 0, 8, "/SUMMON " ) == 0 ) )
{
// lots of code here
}
else if ( ThisPlayer.Character.GMLevel >= 50 && ( StrMessage.compare( 0, 8,
"/jumpto " ) == 0 || StrMessage.compare( 0, 8, "/JUMPTO " ) == 0 ) )
{
// lots of code here
}
etc.. There are actually 30 different else if's. As you can imagine, the
code is quite huge. The simplest thing would to take each // lots of code
here and make them functions and call them. But then I was thinking I could
just pass the GMLevel and StrMessage to the functions themselves. Then I
started thinking about maybe having some type of custom map type container
with the criteria (keyword and GMLevel) and a pointer to the function,
etc...
Over time this is just going to grow. This is the main text processing for
my online game and commands will only get added, not removed over time.
I can think of having a structure/class with Minimim GMLevel, keyword,
pointer to function call. There shouldn't be too much of a problem with
having to pass parameters to the class because of the game system constrains
I have a global structure that I can access what I need anyway.
What would be your suggestion?