View unanswered posts | View active topics It is currently Wed Dec 25, 2024 5:26 pm



Reply to topic  [ 3 posts ] 
 how to create an if-then statement for if a value changes? 
Author Message

Joined: Tue Dec 04, 2012 1:31 am
Posts: 45
Reply with quote
Post 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


Tue Jan 29, 2013 6:13 am
Profile
User avatar

Joined: Fri Feb 16, 2007 8:43 pm
Posts: 1695
Location: AH SHIT FUCK AUGH
Reply with quote
Post 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.


Mon Feb 04, 2013 1:31 am
Profile WWW

Joined: Tue Dec 04, 2012 1:31 am
Posts: 45
Reply with quote
Post 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.


Mon Feb 04, 2013 5:12 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.045s | 13 Queries | GZIP : Off ]