Jump to content
IgorBgz90

Simple Timer

Recommended Posts

If anyone needs, I'll leave it here smile.png

 

Timer:

 if Timer~=nil then return end
 Timer={}

function Timer:Create(name, interval,enable)
 local timer={}
 timer.name = name
 timer.interval = interval
 timer.enable = enable
 timer.time = Time:GetCurrent()+timer.interval
 local k,v
 for k,v in pairs(Timer) do
   timer[k] = v
 end  
 return timer
end

function Timer:Update()
 if self.enable then
   if Time:GetCurrent() > self.time then
  self.time = Time:GetCurrent()+self.interval
  return true
   else
  return false
   end
 end
end

 

How to use:

function App:Start()
 --Timer
 self.time_hours = 0
 self.time_minutes = 0
 self.time_seconds= 0
 self.timer = Timer:Create("timer",1000,true)
end

 

function App:Loop()
 --Very complex calculation time 
 if self.timer:Update() then
   self.time_seconds= self.time_seconds+1
   if self.time_seconds== 60 then
  self.time_minutes = self.time_minutes+1
  if self.time_minutes == 60 then
    self.time_hours = self.time_hours+1
    self.time_minutes = 0
  end
   self.time_seconds= 0
   end
 end

 --Draw current time
 self.context:SetColor(1,1,1,1)
 --Seconds
 local seconds
 if self.time_seconds < 10 then
   seconds = "0"..self.time_seconds
 else
   seconds = self.time_seconds
 end
 --Minutes
 local minutes
 if self.time_minutes < 10 then
   minutes = "0"..self.time_minutes
 else
   minutes = self.time_minutes
 end
 --Hours
 local hours
 if self.time_hours < 10 then
   hours = "0"..self.time_hours
 else
   hours = self.time_hours
 end
 local time_string = hours..":"..minutes..":"..seconds
 self.context:DrawText("Timer: "..time_string,2,2)
end

Share this post


Link to post

Does that work if you have multiple in your game running at the same time? Seems you are just using the 1 Timer table. I can't recall if setting a table to another variable makes a reference to the original (I think it does) or a copy. If you look at AnimationManager.lua you can see how Josh makes a new copy of the class template. It's pretty close to what you have but slight differences like in Create() you just make a new table and assign the variables to that table and then loop over the Timer table getting it's functions and adding them to the new table as well. This will then return a completely separate instance of the class vs I think yours will always return the same instance (Table). Just something to watch out for.

 

Put the below code in http://www.lua.org/cgi-bin/demo to see what is happening. You are returning the 1 Timer table vs making a copy of it.

 

Timer = {}

function Timer:Create(interval)
  local timer = Timer
  timer.interval = interval

  return timer
end

function Timer:Update()
  self.interval = self.interval + 1
end

local timer1 = Timer:Create(500)
local timer2 = Timer:Create(500)

-- I update timer 1
timer1:Update()

-- put timer2 will show 501 because both timer1 and timer2 are pointing to the Timer table. they aren't their own instances. they are "pointers"
print(timer2.interval)

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...