Game Development Community

Client / Server Global Variables

by Tim Heldna · in Torque Game Engine · 08/20/2006 (6:14 am) · 4 replies

Requesting some help from all the Torque networking gurus out there.

I need to register a global variable on the client, either a true or false value, do some stuff based upon that value on the server and only have it affect the client who originally initiated the change in that value.

At the moment I can get two things to happen, both of which are undesirable.

1) Works fine in single player but not at all on network.

2) Works on network however function affects all clients.

If you're not following, here's some simplified pseudo code to illustrate what I need.

client script:
function clientPressedKey(%val)
{
   if(%val)
      $value = true;
   else
      $value = false;
}

server side script:
function myObj::doStuff(%this, %obj)
{
   if($value)
      //do something here if value true which only affects client who called 'clientPressedKey'
   else
      //do something else here if value false which only affects client who called 'clientPressedKey'
}

I've tried using commandToServer, commandToClient, registering the value on only server / client or both and can't quite seem to get it to work.

Any ideas?

#1
08/20/2006 (9:13 am)
I'm certainly no guru with multi-player games, for sure, but what about...

your global variable ($value) could be defined as an array, like $value[0]...$value[%numClients], then in the clientPressedKey function, you would set $value[%currentClient] = true.

In the function myObj::doStuff, you could then iterate through the $value array with a for-next loop, and if $value[%currentIteration] = true, then call commandToClient using %currentIteration as the client to call it on.

Not sure if that makes sense, or if it is what you were looking for...
#2
08/20/2006 (10:51 am)
Why did commandToServer not work,sending $value from client to server in your clientPressedKey function?
Can you post the code you were trying?

-- one thing I have noticed is that you can't receive a global as a function argument. Also, a handy thing you can do (neater than using arrays) is to attach a variable to the client's GameConnection object on the server

Something like this:

client function:
function clientPressedKey(%val)
{
   if(%val)
      $value = true;
   else
      $value = false;
  commandToServer('PressedKey', $value);
}

server function:

function serverCmdPressedKey(%client, %value) 
{
  %client.keyValue = %value;
}

I'm not entirely sure however what you want from your doStuff function: do you want to process all clients that pressed a key? If that's the case, then iterate thru the connected clients and look at their .keyValue variable
#3
09/01/2006 (9:47 am)
Using commandTo*** routine for that kind is not really good idea, as it will eat a lot of traffic (sending command + value).
Have a look at moveMap sources for engine, where referenced $mvForward / $mvSideLeft and others, then implement your own "key" in the engine, and in scripts do:
client:
function clientPressedKey(%val)
{
   $mvMyAction = %val;
}
and on server, do export that value as an attribute for client, so you can do things like:
if (%client.myAction) doSomething(%client);
or even force script callback from engine into scripts:
function gameConnection::myActionStateChanged(%this,%value)
{
   // do what you want to do with client who pressed a KEY
}
......... hope it's understandable what I wrote :) havn't slept well lately.. :)
good luck!
#4
09/01/2006 (9:55 am)
Hey thanks, that's exactly what I ended up doing. I actually solved this a long time ago, was just too tired on the night I made my original post here ;)

Thanks again to all who helped.