PDA

View Full Version : Distinguishing when a player spawns and despawns.



SoW Reddog
Mar-30-2014, 18:26
Just a quickie, what's the best way of distinguishing when a player spawns into an aircraft, rather than using OnPlaceEnter which triggers when a player moves from position to position in a bomber for example.

I need to make sure that code only runs once. I'd prefer to do it using inbuilt methods and functions than creating yet another data holding system and then having to remove from it on a variety of different situations.

Salmo
Mar-31-2014, 05:25
Does this help?



public override void OnActorCreated(int missionNumber, string shortName, AiActor actor)
{
base.OnActorCreated(missionNumber, shortName, actor);

AiAircraft aircraft = actor as AiAircraft;
if (aircraft != null) // the spawning actor is an aircraft
{
Timeout(1.0, () => // wait for human to load into the plane
{
if ((aircraft.Player(0) != null) // a human is in the pilot seat
{
// custom code here for what you want
}
});
}
}

public override void OnPlaceLeave(Player player, AiActor actor, int placeIndex)
{
base.OnPlaceLeave(player, actor, placeIndex);

AiAircraft aircraft = actor as AiAircraft;
if (aircraft != null) // the player is changing places (or despawning) an aircraft
{
Timeout(1.0, () => // wait a moment for player to change place or leave aircraft
{
if (isAiControlledPlane(aircraft) && IsAtAirfield(aircraft)) // no humans in plane; plane at airfield & not moving
{
// custom code here
}
});

}
}

public bool IsAtAirfield(AiAircraft aircraft)
{ // true if plane is on the ground & stationary at an airfield
bool result = false;
if (aircraft != null) // a valid aircraft object
{
if (aircraft.getParameter(ParameterTypes.Z_VelocityTA S, 0) <= 0.01) // plane is stationary
{
AiAirport[] airports = GamePlay.gpAirports();
for (int i = 0; i < airports.Length; i++)
{
Point3d p = airports[i].Pos(); // remember the airport map position
if (aircraft.Pos().distance(ref p) <= airports[i].CoverageR()) // aircraft is inside the airfield radius
{
result = true;
}
}
}
}
return result;
}

private bool isAiControlledPlane(AiAircraft aircraft)
#region Returns whether aircraft is an Ai plane (no humans in any seats)
{ // returns true if specified aircraft is AI controlled with no humans aboard, otherwise false
if (aircraft == null) return false;
//check if a player is in any of the "places"
for (int i = 0; i < aircraft.Places(); i++)
{
if (aircraft.Player(i) != null) return false;
}
return true;
}
#endregion

SoW Reddog
Mar-31-2014, 06:56
Hugely. I'd worked out a massively convoluted method in my head. Thanks.