Author Topic: Getting + setting a players position  (Read 1215 times)

Im trying to make a command that will switch the locations of both players... but it doesn't work
function servercmdSwap(%client, %target)
{
%pos1 = %client.getObject.getPosition();
%pos2 = %target.getObject.getPosition();
%client.getObject.setTransform(%pos2);
%target.getObject.setTransform(%pos1);
}

could someone help?

uhh

let me try rewriting this for you

i made it small and fast
i don't know if it will work but if it doesn't you should be able to work off of it.

Code: [Select]
function serverCmdSwap(%this, %target)
{
%t = findClientByName(%target);
if(isObject(%t) && isObject(%t.player) && isObject(%this.player))
{
%pos1 = %t.player.getTransform();
%pos2 = %this.player.getTransform();

%t.player.setTransform(%pos2);
%this.player.setTransform(%pos1);
}
}

-snip-
I don't see anything wrong with this.
But checking if %t is an object is unnecessary

I don't see anything wrong with this.
But checking if %t is an object is unnecessary
That is making sure the player exists

That is making sure the player exists

No, it's making sure the client exists. If the client's player doesn't exist, the client doesn't exist either anyway. It won't cause any error messages unless $Con::warnUndefinedVariables is true.


Code: [Select]
function serverCmdSwap(%this, %target)
{
%t = findClientByName(%target);
if(isObject(%t) && isObject(%t.player) && isObject(%this.player))
{
%pos1 = %t.player.getTransform();
%pos2 = %this.player.getTransform();

%t.player.setTransform(%pos2);
%this.player.setTransform(%pos1);
}
}
That doesnt work...



Also, remove isObject(%t)
It is, as previously mentioned, unneeded.
It won't make any noticeable performance hit, but it is a good habit to write things as efficient as possible, and removing redundant function checks is one way to do so

Code: [Select]
function serverCmdSwap(%this, %target)
{
%t = findClientByName(%target);
if(isObject(%t.player) && isObject(%this.player))
{
%pos1 = %t.player.getTransform();
%pos2 = %this.player.getTransform();

%t.player.setTransform(%pos2);
%this.player.setTransform(%pos1);
}
}