Game Development Community

Monitoring Client Position Changes on Server

by Welias D. Willie II · in Torque Game Engine · 08/30/2005 (6:58 pm) · 4 replies

What is the best way to trigger an event on the server when a clients position changes? (Not just when a client moves)

currently I am trying something like this:

function ClientPositionMonitor()
{

    %count = ClientGroup.getCount();
    if (%count > 0)
    {
	for ( %ci = 0; %ci < %count; %ci++ )
	{
		%client = ClientGroup.getObject( %ci );
		if (%client > 0)
		{	
		    if(isObject(%client.player.getTransform()))  // returning error
		    {
			%pos = %client.player.getTransform();
			%newx = mFloor((getWord(%pos,0)/100));
			%newy = mFloor((getWord(%pos,1)/100));

			%currx = mFloor(getWord(%client.player.location,0));
			%curry = mFloor(getWord(%client.player.location,1));
		
			if (%newx != %currx || %newy != %curry)
			{
				%currx = %newx;
				%curry = %newy;
				
				%client.player.location = %currx SPC %curry;
				
				echo("*** POSITION MONITOR: CLIENT:" SPC %client SPC "MOVED TO: X" SPC %currx SPC "Y" SPC %curry);
                                // execute some function based on client movements
			}
		   }
		}
	}
    }
schedule(1000,0,"ClientPositionMonitor");
}

I execute this code as soon as the server launchs (in my common/server/game.cs). It seems like there would be a more optimized solution where events are kicked off based on movement or maybe something I haven't thought of, rather than executing a function every second to check client's positions on the server.

Also, if the code snippet above is acceptable, I am getting an error I would like to trap (Which is why I am checking every variable in the function above). The error happens when a client is issued a client id on the server side but has not fully entered the game therefore does not have a position for getTransform to report on. So in between "Mapping string: MissionStartPhase1Ack to index : 0" and "Mapping string: MissionStartPhase2Ack to index : 1" I get this error:

Unable to find object: ' ' attempting to call function 'getTransform'

Like I mentioned if the code I am using above is worth keeping, then I simply want some way to trap the error I am getting above, however, I feel there is probably a more efficient way to monitor and react to client position changes.

Suggestions?

#1
08/30/2005 (7:43 pm)
Are you working in script only, or do you modify source code? If so, I'd simply put your functionality in player.cc--processTick(), or even possibly updateMove().
#2
08/30/2005 (7:51 pm)
Stephen, I originally posted this in the private forum, then moved it here, now I went back and posted it again in the thread I had originally created.
#3
08/30/2005 (8:32 pm)
I think you could avoid the error by changing this:

if(isObject(%client.player.getTransform()))

to

if(isObject(%client.player))

i think for an existing client you want to check whether a player object exists before you call 'gettransform()' on it.
#4
09/08/2005 (5:37 pm)
Thanks Sean... if(isObject(%client.player)) did the trick.