Data Realms Fan Forums
http://forums.datarealms.com/

Help needed: Error in lua script. [Solved]
http://forums.datarealms.com/viewtopic.php?f=73&t=25611
Page 1 of 1

Author:  Gotcha! [ Thu Sep 22, 2011 1:41 pm ]
Post subject:  Help needed: Error in lua script. [Solved]

The following script:

Code:
function UniTecSMGSwitchMain(actor)
   local gun = ToAHuman(actor).EquippedItem;
   if gun ~= nil then
      gun.Sharpness = 1;
   end
end
function Create(self)
   self.rechargeTimer = Timer()
   self.rechargeDelay = 70
   self.manualmaxammo = 30
   self.Magazine.RoundCount = self.manualmaxammo
   self.AIChargeRounds = 5
   self.AIChargeTimer = Timer()
   self.AIChargeDelay = self.rechargeDelay * self.AIChargeRounds
   self.weaponNameTable = {"UniTec SMG"};
   self.weaponClassTable = {"HDFirearm"};
   self.switchPhase = 0;
   self.switchTimer = Timer();
   self.flipTimer = false;
   self.switchDelay = 1500;
end

function Update(self)

   if self.Magazine ~= nil then
      if MovableMan:GetMOFromID(self.RootID):IsActor() then
         self.z = ToActor(MovableMan:GetMOFromID(self.RootID))
         if self.z:IsPlayerControlled() == false then
            if self.AIChargeTimer.ElapsedSimTimeMS < self.AIChargeDelay then
               self.z:GetController():SetState(Controller.WEAPON_FIRE,false)
            end
            if self.Magazine.RoundCount == 1 then
               self.AIChargeTimer:Reset()
            end
         else
            self.AIChargeTimer:Reset()
         end
      end
      if self:IsActivated() then
         self.rechargeTimer:Reset()
      else
         if self.Magazine.RoundCount < self.manualmaxammo and self.rechargeTimer:IsPastSimMS(self.rechargeDelay) then
            self.rechargeTimer:Reset()
            local ammoCheck = self.Magazine.RoundCount + 1
            self.Magazine.RoundCount = ammoCheck
         end
      end
      if self.Magazine.RoundCount == 1 then
         self:Deactivate()
      end
   end
   if self.Sharpness ~= 0 then
      local actor = MovableMan:GetMOFromID(self.RootID);
      if MovableMan:IsActor(actor) then
         self.parent = ToAHuman(actor);
      end
      if self.flipTimer == false then
         self.flipTimer = true;
         self.switchTimer:Reset();
      end
      if self.switchTimer:IsPastSimMS(self.switchDelay) then
         if self.switchPhase == 0 then
            if MovableMan:IsActor(self.parent) then
               if self.weaponClassTable[self.Sharpness] == "HDFirearm" then
                  local switchGun = ToActor(self.parent):AddInventoryItem(CreateHDFirearm(self.weaponNameTable[self.Sharpness]));
               elseif self.weaponClassTable[self.Sharpness] == "TDExplosive" then
                  local switchGun = ToActor(self.parent):AddInventoryItem(CreateTDExplosive(self.weaponNameTable[self.Sharpness]));
               elseif self.weaponClassTable[self.Sharpness] == "HeldDevice" then
                  local switchGun = ToActor(self.parent):AddInventoryItem(CreateHeldDevice(self.weaponNameTable[self.Sharpness]));
               end
               ToActor(self.parent):GetController():SetState(Controller.WEAPON_DROP,true);
               self.switchPhase = 1;
            else
               self.Sharpness = 0;
            end
         else
            if MovableMan:IsActor(self.parent) then
               if ToActor(self.parent):IsInventoryEmpty() == false then
                  ToActor(self.parent):GetController():SetState(Controller.WEAPON_CHANGE_PREV,true);
               end
            end
            self.ToDelete = true;
         end
      else
         if MovableMan:IsActor(self.parent) then
            ToActor(self.parent):GetController():SetState(Controller.WEAPON_FIRE,false);
         end
      end
   end
end

gives the following error:
ERROR: UniTec.rte/Devices/Lua/UniTec SMG Switch.lua:26: attempt to index a nil value.

It has to do with the recharging bit, all recharging weapons in UniTec give this error.
The weapon itself works fine though.

I could leave it as it is, since there are no functional problems or anything, but I have to admit that the error bothers me. :lol:

Author:  TheLastBanana [ Thu Sep 22, 2011 2:45 pm ]
Post subject:  Re: Help needed: Error in lua script.

I think your problem is this line:
Code:
if MovableMan:GetMOFromID(self.RootID):IsActor() then

IsActor is called on the MO itself, so if the MO is nil, you'll get that error. Just put in a check that it isn't, and that should fix the problem.

Author:  Gotcha! [ Thu Sep 22, 2011 3:11 pm ]
Post subject:  Re: Help needed: Error in lua script.

Pardon my ignorance, but what would I change/add to do that? :)

Author:  Coops [ Thu Sep 22, 2011 4:04 pm ]
Post subject:  Re: Help needed: Error in lua script.

Code:
if self.z ~= nil then
    if MovableMan:IsActor(self.z) then
        -- all your stuff
    end
else
    self.z = MovableMan:GetMOFromID(self.RootID)
end

Author:  Gotcha! [ Thu Sep 22, 2011 4:17 pm ]
Post subject:  Re: Help needed: Error in lua script.

Thanks, Coops! :grin: All working now.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/