View unanswered posts | View active topics It is currently Fri Jul 19, 2019 11:27 am



Reply to topic  [ 5 posts ] 
 Help needed: Error in lua script. [Solved] 
Author Message
User avatar

Joined: Tue Apr 01, 2008 4:49 pm
Posts: 1966
Location: The Netherlands
Reply with quote
Post 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:


Last edited by Gotcha! on Thu Sep 22, 2011 4:51 pm, edited 1 time in total.



Thu Sep 22, 2011 1:41 pm
Profile
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3134
Location: A little south and a lot west of Moscow
Reply with quote
Post 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.


Thu Sep 22, 2011 2:45 pm
Profile WWW
User avatar

Joined: Tue Apr 01, 2008 4:49 pm
Posts: 1966
Location: The Netherlands
Reply with quote
Post Re: Help needed: Error in lua script.
Pardon my ignorance, but what would I change/add to do that? :)


Thu Sep 22, 2011 3:11 pm
Profile
User avatar

Joined: Wed Feb 17, 2010 12:07 am
Posts: 1545
Location: That small peaceful place called Hell.
Reply with quote
Post 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


Thu Sep 22, 2011 4:04 pm
Profile
User avatar

Joined: Tue Apr 01, 2008 4:49 pm
Posts: 1966
Location: The Netherlands
Reply with quote
Post Re: Help needed: Error in lua script.
Thanks, Coops! :grin: All working now.


Thu Sep 22, 2011 4:17 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.017s | 13 Queries | GZIP : Off ]