Author Topic: Useful function for OnClientEnter event  (Read 574 times)

Legacy_Melkior_King

  • Full Member
  • ***
  • Posts: 234
  • Karma: +0/-0
Useful function for OnClientEnter event
« on: February 08, 2011, 06:28:01 am »


               Many servers are set up so that on entry to the server, the player is automatically jumped to the starting location.  Doing this prevents several potential problems.

The problem is that if the player is already at the start location, the start location will load twice which is a waste of time.  The following function gets around that problem.  Place it before the void main() function in your OnClientEnter script.
[nwscript]
// Waits until the player is in a valid area then, if the player is not
// at the starting location, forces the player to jump to it.
void JumpToStart(object oPlayer=OBJECT_SELF);
void JumpToStart(object oPlayer=OBJECT_SELF)
{
  location lStart = GetStartingLocation();
  if (!GetIsObjectValid(GetArea(oPlayer)))
  {
    DelayCommand(0.1,AssignCommand(oPlayer,JumpToStart(oPlayer)));
    return;
  }
  if (GetArea(oPlayer)!=GetAreaFromLocation(lStart)
    || GetDistanceBetweenLocations(GetLocation(oPlayer),lStart)>5.0)
    AssignCommand(oPlayer,ActionJumpToLocation(lStart));
}[/nwscript]
In your OnClientEnter event, you need to find the entering player and assign the JumpToStart command to the player.
[nwscript]
object oPC=GetEnteringObject();
AssignCommand(oPC,JumpToStart(oPC));[/nwscript]
Strictly speaking, there's no need to specify the PC in the function call since the function uses OBJECT_SELF as the default, which should automatically select the player.