I have the following situation:
void function(params)
{
#ifdef _MULTIPLAYER
if (isConnected)
{
if (isClient)
{
requestFunctionRemotely(params)
return;
}
else if (isServer)
{
call realFunction(params) remotely on client;
}
}
#endif
realFunction(params);
}
realFunction(params)
{
...
}
With this situation I’m able to build my project either with single player instance or with multiplayer.
This approach has been used because function(params) is called all around my code, so in this way I just attach to the lowest possible level without having problems.
I had to split the function and realFunction because remote responses received by clients must be executed through realFunction to avoid loops (I could change the function prototype to add a flag but I would break a lot of code).
My concern is related to performance when disabling the _MULTIPLAYER define. I have A LOT of function(params) and I’ll call them thousand of times. I’ll have to use this approach for all of them. Will g++ optimize away the double call when it will find
function(params) {
realFunction(params);
}
to
function(params) {
// body of realFunction
}
and there is way to force it in every similar situation?
Just as a hint: I currently use the -O2 flag
If you want to be sure you could invert your pattern and use a template parameter. That way you are sure the compiler will remove the
if (false)at compile time.Note:
If working with a class, you could move up the template parameter to the class for concision.