Game Development Community

How to know whether the codes running as a server or a client?

by Huan Li · in Torque Game Engine · 02/22/2009 (12:46 am) · 5 replies

I want to optimize the script function related to "CONTAINERS and RAYCASTS", because i found, in engine, all of these functions use gServerContainer for calculating.So i want to modify this.I need to know whether the app is running as a server or a client, then i can decide to use gServerContainer or gClientContainer.
For example like below:
{
...
  if( gIsServer == true )
     return gServerContainer(...);
  else
     return gClientContainer(...);
...
}
I want to know, is there some global var like "gIsSever" in the upper codes already existing in the orignal TGEA source code? Or can i get this info in some other way?
Thanx for any uesful info:)

#1
02/22/2009 (1:37 am)
I'm not too sure that you've got the right idea with this Huan, the castRay functions by default are all server side functions and are implemented that way to stop people cheating and because that's where they are best used.

If you move them client side then people can interfere with the results and break your game so be careful, however there are some advantages to using a client side raycast - namely easing the load.

For example:
- Ray Cast on client to search for items to pickup.
- When found convert object ID into Ghost Index
- Send a message from client to server to perform an action on the Ghost index

- Server receives message and translates Ghost Index into Server Object ID
- Server ray casts to check that the action is valid.
- Server processes action if valid.

Now you could change the raycast script function to accept whether being called on server or client, or just create a second copy of the function - either way you're controlling whether it's being called on the client or server.
#2
02/22/2009 (2:24 am)
Thanx Andy,your advices is just what i am thinking about, i won't let client do critical work, just want to help server do some calculating.
The validation is on server side,just like you said.

Another but the same question, is there a variable like "gIsServer" in engine codes which can tell whether the current App is a server or a client?
#3
02/22/2009 (2:36 am)
If there's no such variable like "gIsServer", i want to code myself in this way:
in source code,i'll add the following codes
bool gIsServer = false;
Con::addVariable( "IsServer", TypeBool, &gIsServer );
and something more like:
{
...
  if( gIsServer == true )
     return gServerContainer(...);
  else
     return gClientContainer(...);
...
}
in script,locate the function "createServer(...)",add a line like below
function createServer(...)
{
  ...
  
  if (%serverType $= "MultiPlayer") {
      portInit($Pref::Server::Port);  
      $IsServer = true;   //this is the new line i added.
  }
  ...
}
How do you think about it, all my friends?
#4
05/14/2009 (11:10 am)
Huan, every class that derives from NetObject (ex: SceneObject and all its derivatives) has two methods called isServerObject() and isGhost() that allow you to know if that object is on a server or if it's a ghost.

If you're willing to have a console function (not a method) that behaves differently on server and on client it's safer to have two functions: one for client and another for the server, like this:

void gWeirdFunction(Container &container)
{
...
}

void gServerWeirdFunction()
{
   gWeirdFunction(gServerContainer);
}

void gClientWeirdFunction()
{
   gWeirdFunction(gClientContainer);
}

If you're calling them from script you can can call the correct versions in the correct places, since the execution paths for server and client scripts are distinct. Always avoid mixing server and client stuff, since it can bite you later.
#5
05/14/2009 (11:05 pm)
@Maneol
Thank you, i'll try your way.