PDA

View Full Version : Identifying if an aircraft is AI controlled



SoW Reddog
Nov-09-2013, 09:03
Hi all, Salmo gave me this function



#region Returns whether aircraft is an Ai plane (no humans in any seats)
private bool isAiControlledPlane(AiAircraft aircraft)

{ // 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

Problem is, that it doesn't seem to work correctly in the following

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

AiAircraft a = actor as AiAircraft;
if (a != null && isAiControlledPlane(a))
{
//do stuff because it's an AI controlled plane
}
} as it's definitely adding in the players spawning in. I don't understand why though.

Salmo
Nov-09-2013, 17:30
OnActotCreated is fired when the plane spawns. At this time, the plane does not contain the human pilot, so the isAiControlledPlane function returns true (no human in plane) falsely suggesting the plane is Ai controlled. But if you test 1-2 seconds after the plane spawns, the human will have been loaded into the plane & the isAiControlledPlane function will then return false (human in plane).



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

AiAircraft a = actor as AiAircraft;
Timeout(1.0, () => // wait 1 second for human to load into plane
{
if (a != null && isAiControlledPlane(a))
{
//do stuff because it's an AI controlled plane
}
});
}

SoW Reddog
Nov-10-2013, 05:34
Thanks Salmo, that sorted it straight out. I had a timeout in my code, but further into the logic path!