Script causes crashes when exiting/reloading
So I made this neato pilotable vehicle script, which allows an actor to be piloted by other ones. It's pretty stable, and works exactly how I wanted it to.
However.
When the actor is being piloted, if the scene ends or is reloaded, the game will crash pretty much instantly. I've tried to avoid this with extra ActivityRunning() and self:IsPlayerControlled checks, but to no avail. So I'm asking ye, the lua-masters of DRL, if you would kindly help me. Here's the script:
Code:
function Create(self)
self.IsPiloted = 0;
self.Pilot = nil;
self.DeathTimer = Timer();
self.timer = Timer();
self.SignTimer = Timer();
self.EffectTimer = Timer();
self.IsDying = 0;
self.PilotPosY = SceneMan.SceneHeight;
self.Placer = nil;
for i = 1,MovableMan:GetMOIDCount()-1 do
self.finder = MovableMan:GetMOFromID(i);
if self.finder.PresetName == "Apollo Armor Suit Effect Placer" and self.finder.RootID == self.ID then
self.Placer = ToAttachable(MovableMan:GetMOFromID(self.finder.ID));
print(("Found placer: ") .. (self.Placer.PresetName));
end
end
end
function Update(self)
if self.timer:IsPastSimMS(25) then
if ActivityMan:ActivityRunning() then
if self.IsPiloted == 0 then
if self.SignTimer:IsPastSimMS(20) then
local sign = CreateMOSParticle("No Pilot Sign");
sign.Pos = self.AboveHUDPos;
MovableMan:AddParticle(sign);
self.SignTimer:Reset();
end
self:SetControllerMode(Controller.CIM_DISABLED, -1);
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 < 50 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;
self:SetControllerMode(Controller.CIM_AI, -1);
ActivityMan:GetActivity():SwitchToActor(self,actor:GetController().Player,self.Team);
self:FlashWhite(1000);
actor.HitsMOS = false;
actor.GetsHitByMOS = false;
actor.AIMode = Actor.AIMODE_SENTRY;
actor:SetControllerMode(Controller.CIM_DISABLED, -1);
self.HitsMOS = true;
end
end
end
end
self.AIMode = Actor.AIMODE_SENTRY;
else
if self:GetController():IsState(Controller.MOVE_UP) then
if self.EffectTimer:IsPastSimMS(10 + math.random(1,10)) then
local smoke = CreateMOSParticle("Side Thruster Blast Ball 1");
smoke.Pos = self.Placer.Pos;
smoke.Vel = self.Vel + ((smoke.Pos - self.Pos) * (math.random(4,9)/10));
MovableMan:AddParticle(smoke);
self.EffectTimer:Reset();
end
elseif self:GetController():IsState(Controller.MOVE_LEFT) or self:GetController():IsState(Controller.MOVE_RIGHT) then
if self.EffectTimer:IsPastSimMS(30 + math.random(1,50)) then
local smoke = CreateMOSParticle("Small Smoke Ball 1");
smoke.Pos = self.Placer.Pos;
smoke.Vel = self.Vel + ((smoke.Pos - self.Pos) * (math.random(2,6)/10));
MovableMan:AddParticle(smoke);
self.EffectTimer:Reset();
end
else
if self.EffectTimer:IsPastSimMS(30 + math.random(1,50)) then
local smoke = CreateMOSParticle("Tiny Smoke Ball 1");
smoke.Pos = self.Placer.Pos;
smoke.Vel = self.Vel + ((smoke.Pos - self.Pos) * (math.random(1,5)/10));
MovableMan:AddParticle(smoke);
self.EffectTimer:Reset();
end
end
if MovableMan:IsActor(self.Pilot) then
self.Pilot.Pos.X = 5;
self.Pilot.Pos.Y = self.PilotPosY;
self.Pilot.Vel = Vector(0,0);
self.Pilot.AngularVel = 0;
self.Pilot.HitsMOS = false;
self.Pilot.GetsHitByMOS = false;
self.Pilot:SetControllerMode(Controller.CIM_DISABLED, -1);
if self.Pilot:IsPlayerControlled() then
ActivityMan:GetActivity():SwitchToActor(self,self.Pilot:GetController().Player,self.Team);
end
if self.AIMode == Actor.AIMODE_GOLDDIG and self:IsPlayerControlled() and self: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;
self.Pilot:SetControllerMode(Controller.CIM_AI, -1);
if self:IsPlayerControlled() then
ActivityMan:GetActivity():SwitchToActor(self.Pilot,self:GetController().Player,self.Team);
end
self.Pilot = nil;
self.IsPiloted = 0;
self.HitsMOS = false;
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;
self.Pilot:SetControllerMode(Controller.CIM_AI, -1);
if self:IsPlayerControlled() then
ActivityMan:GetActivity():SwitchToActor(self.Pilot,self:GetController().Player,self.Team);
end
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
end
function Destroy(self)
if ActivityMan:ActivityRunning() then
if self.IsPiloted == 1 then
if MovableMan:IsActor(self.Pilot) then
self.Pilot.Pos = self.Pos + Vector(5,5);
self.Pilot:FlashWhite(500);
self.Pilot.HitsMOS = true;
self.Pilot.GetsHitByMOS = true;
self.Pilot:SetControllerMode(Controller.CIM_AI, -1);
if self:IsPlayerControlled() then
ActivityMan:GetActivity():SwitchToActor(self.Pilot,self:GetController().Player,self.Team);
end
self.Pilot = nil;
end
end
end
end