Ok I can confirm after testing it is a double up on one object, being an Aiaircraft
The problem is because I have applied two options for despawning aiaircraft,
one is via Ontaskcompleted or final path point, then I run
Timeout(despawntime2, () => { damageAiControlledPlane(actor); });
However I also run OnActorcreated upon the same object
Timeout(despawntime, () => { damageAiControlledPlane(actor); });
So hence I am trying to run the despawner upon the same object twice, in early testing the nullpointer exception did not show up, which is kinda random reaction.
The theory for why I use time periods is to allow aiplane use for players, but also I want to despawn for short flight path aiaircraft, since the timer period is 45~60 minutes.
So choice is, rely on one timer or somehow purge the remaining object timer
Usually the problem is overall timer as this is longer, but either can also overlap.
Kodiak I see how to reference a list object by null, but how to reference this aiaircraft as it is the timer acting upon the ai aircraft.
Code:
#region Despawner
/*
despawner timer added to original code, sets overall time period for actors,
Advantage is, if delayed, say trying to land behind masses of planes, actors despawn at timer period set anyway.
allows for players to fly groups of planes,
Player can re eneter flights or groups, as bombers take them to target, use each at target then fly group home
despawn only triggers once player leaves whole group
*/
public override void OnActorCreated(int missionNumber, string shortName, AiActor actor)
{
base.OnActorCreated(missionNumber, shortName, actor);
// note despawntime (set as varible at start of cs)
if (actor is AiGroundActor && missionNumber > 0)//main mission is left alone
//if ((actor as AiGroundActor).Type() != maddox.game.world.AiGroundActorType.AAGun)//all ai ground except aa guns
Timeout(despawntime3, () =>
{
if (actor != null)
{ (actor as AiGroundActor).Destroy(); }
});
if (actor is AiAircraft)
Timeout(despawntime, () => { damageAiControlledPlane(actor); });
}
public override void OnPlaceLeave(Player player, AiActor actor, int placeIndex)
{
base.OnPlaceLeave(player, actor, placeIndex);
Timeout(despawntime, () => { damageAiControlledPlane(actor); });
}
// Path completion despawner, when flight path ends despawner action triggered
// note:final despawner detects players in group
public override void OnActorTaskCompleted(int missionNumber, string shortName, AiActor actor)
{
base.OnActorTaskCompleted(missionNumber, shortName, actor);
if (actor is AiGroundActor)
if (actor != null)
Timeout(despawntime2, () => { (actor as AiGroundActor).Destroy(); });
Timeout(despawntime2, () => { damageAiControlledPlane(actor); });
}
//check if a player is in any of the "places"
private bool isAiControlledPlane(AiAircraft aircraft)
{
if (aircraft == null) return false;
for (int i = 0; i < aircraft.Places(); i++)
if (aircraft.Player(i) != null)
return false;
return true;
}
private void destroyPlane(AiAircraft aircraft)
{
if (aircraft != null)
aircraft.Destroy();
}
//actual Aiaircraft despawner, note it polls player detection
private void damageAiControlledPlane(AiActor actorMain)
{
foreach (AiActor actor in actorMain.Group().GetItems())
{
if (actor == null || !(actor is AiAircraft))
return;
AiAircraft aircraft = (actor as AiAircraft);
if (!isAiControlledPlane(aircraft))
return;
if (aircraft == null)
return;
(actor as AiAircraft).Destroy();
}
}
#endregion
/*=========================================*/