Data Realms Fan Forums
http://45.55.195.193/

how to create an if-then statement for if a value changes?
http://45.55.195.193/viewtopic.php?f=73&t=45097
Page 1 of 1

Author:  nubbles [ Tue Jan 29, 2013 6:13 am ]
Post subject:  how to create an if-then statement for if a value changes?

I'm trying to write a health regeneration script that works rather COD style-ish, and I already have the basic stuff done, I just can't figure out how to check for if health changes, going down. I have it so it resets the timer if health is at 100, making it so there is a delay if he is hit once, but it isn't perfect. How can I check for value changes with an if then statement?

EDIT: I managed it, sorta. Tell me if I can make it more streamlined:
Code:
require("Actors/AI/NativeHumanAI")
function Create(self)
   self.AI = NativeHumanAI:Create(self)
    --Keep track of how long it should be before healing people.
   self.healATimer = Timer();
    --Interval between healings, in milliseconds.
   healAInterval = 8000;
    --Heal counter.
   self.lifeCTimer = Timer();
   lifeCInterval = 200;
   self.healActualTimer = Timer();
   healActualInterval = 400;
   self.actorHP = self.Health;
end

function Update(self)
   if self.Health < 100 then
      if self.healATimer:IsPastSimMS(healAInterval) then
         if self.healActualTimer:IsPastSimMS(healActualInterval) then
            self.Health = self.Health + 1;
            self.actorHP = self.Health;
            self.lifeCTimer:Reset();
            self.healActualTimer:Reset();
         end
      end
   end
   if self.lifeCTimer:IsPastSimMS(lifeCInterval) then
      self.actorHP = self.Health;
      self.lifeCTimer:Reset();
   end
   if self.Health >= 100 or self.actorHP > self.Health then
      self.healATimer:Reset();
      self.lifeCTimer:Reset();
      self.actorHP = self.Health;
   end
end

function UpdateAI(self)
   self.AI:Update(self)
end

Author:  Shook [ Mon Feb 04, 2013 1:31 am ]
Post subject:  Re: how to create an if-then statement for if a value changes?

Hard to make more streamlined than that, really. The only possible thing would be that you aren't checking for positive health changes, but given what you're using it for, that shouldn't be an issue. I think that lifeCTimer is redundant, however, since it just equalizes health and actorHP every 200 milliseconds.

If i were to give an untested(!!!) take on it (and if you're even at all interested), here you go:
Code:
function Create(self)
   self.oldHealth = self.Health;
   self.healDelayTimer = Timer();
   self.healDelay = 8000;
   self.healTimer = Timer();
   self.healInterval = 400;
end

function Update(self)
   local healthDiff = self.Health - self.oldHealth;
   if healthDiff < 0 then
      self.healDelayTimer:Reset();
   end
   if self.healDelayTimer:IsPastSimMS(self.healDelay) and self.Health < 100 then
      if self.healTimer:IsPastSimMS(self.healInterval) then
         self.Health = self.Health + 1;
         self.healTimer:Reset();
      end
   end
   self.oldHealth = self.Health;
end

This is only a tiny bit shorter, and the only functional difference is that it has a variable for the health difference in the current frame, which could potentially be used for more shenanigans.

So yeah, to answer your original question: Pretty much how you did it. Store the previous value in a different variable, and compare it to the current value.

Author:  nubbles [ Mon Feb 04, 2013 5:12 pm ]
Post subject:  Re: how to create an if-then statement for if a value changes?

Hey, thanks for replying. I'm rather new to the world of lua scripting, an it's always good to get opinions on work. I realized that lifeCTimer() is redundant, but when I removed it ye script ceased working. I may have done something wrong. It's also good that data realms has comments on basically every script they have in game, it's amazing for helping me get started.

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