Sudden crash when swapping actors
Very well. I decided to try my hand at making a pilotable vehicle script, and after a few hours of ironing out bugs, came up with this:
Code:
function Create(self)
self.IsPiloted = 0;
self.Pilot = nil;
self.DeathTimer = Timer();
self.timer = Timer();
self.IsDying = 0;
end
function Update(self)
if self.timer:IsPastSimMS(25) then
if self.IsPiloted == 0 then
if self:IsPlayerControlled() then
ActivityMan:GetActivity():SwitchToActor(ActivityMan:GetActivity():GetPlayerBrain(self:GetController().Player),self:GetController().Player,self.Team);
end
for actor in MovableMan.Actors do
if actor.ClassName == "AHuman" and actor.Mass < 300 then
local avgx = actor.Pos.X - self.Pos.X;
local avgy = actor.Pos.Y - self.Pos.Y;
local dist = math.sqrt(avgx ^ 2 + avgy ^ 2);
if dist < 30 then
if actor:IsPlayerControlled() and actor:GetController():IsState(Controller.PIE_MENU_ACTIVE) and actor.AIMode == Actor.AIMODE_GOLDDIG then
self.Pilot = actor;
self.IsPiloted = 1;
ActivityMan:GetActivity():SwitchToActor(self,self:GetController().Player,self.Team);
actor.HitsMOS = false;
actor.GetsHitByMOS = false;
actor.AIMode = Actor.AIMODE_NONE;
end
end
end
end
self.AIMode = Actor.AIMODE_NONE;
else
if MovableMan:IsActor(self.Pilot) then
self.Pilot.Pos.X = 0;
self.Pilot.Pos.Y = 0;
self.Pilot.Vel = Vector(0,0);
self.Pilot.AngularVel = 0;
if self.Pilot:IsPlayerControlled() then
ActivityMan:GetActivity():SwitchToActor(self,self:GetController().Player,self.Team);
end
if self.AIMode == Actor.AIMODE_GOLDDIG and self:IsPlayerControlled() and actor:GetController():IsState(Controller.PIE_MENU_ACTIVE) then
self.Pilot.Pos = self.Pos + Vector(5,5);
self:FlashWhite(500);
self.Pilot:FlashWhite(1000);
self.Pilot.HitsMOS = true;
self.Pilot.GetsHitByMOS = true;
ActivityMan:GetActivity():SwitchToActor(self.Pilot,self:GetController().Player,self.Team);
self.Pilot = nil;
self.IsPiloted = 0;
end
if self.Health < 2 then
self.Pilot.Pos = self.Pos + Vector(5,5);
self:FlashWhite(500);
self.Pilot:FlashWhite(500);
self.Pilot.HitsMOS = true;
self.Pilot.GetsHitByMOS = true;
ActivityMan:GetActivity():SwitchToActor(self.Pilot,self:GetController().Player,self.Team);
self.Pilot = nil;
self.IsPiloted = 0;
self.IsDying = 1;
end
else
self.Pilot = nil;
self.IsPiloted = 0;
end
end
if self.IsDying == 0 then
self.DeathTimer:Reset();
end
if self.Health < 2 then
self.IsDying = 1;
end
if self.DeathTimer:IsPastSimMS(1000) then
self:GibThis();
end
self.timer:Reset();
end
end
CC crashes instantly when I turn AI to gold dig on the pie menu. I assume it must be the game trying to refer to a null entry, but I can't see where the code would allow that to happen. It also gives no error message whatsoever, just stops working. Any thoughts on this?