Timed jobs

Hello,

The player has a “bank account”. They spend money on their store and receive money from customers.

They also receive money on a “daily” (of course it’s not realtime) basis from interest.

What’s the best way to automatically add to an integer every X seconds? What’s the best way to have an ongoing runtime for actions to take place that are not initiated by the user?

Thanks! [import]uid: 85686 topic_id: 14209 reply_id: 314209[/import]

Hey.

There are several ways you can go about this.
The “Best” way can really only be decided by you. However here is one example :

  
local gameTime = 0  
local fullDay = 10000  
local incrementTimer = true  
  
local function updateGame(event)  
 --Update game time  
 if incrementTimer == true then  
 gameTime = gameTime + 1  
 end  
  
 --If the full day has been reached  
 if gameTime \>= fullDay then  
 incrementTimer = false  
 --do whatever, update account etc  
 --Reset day  
 gameTime = 0  
 end  
end  
  
Runtime:addEventListener("enterFrame", updateGame)  

[import]uid: 84637 topic_id: 14209 reply_id: 52401[/import]

Nice code! Makes sense. ‘enterFrame’ at Runtime is useful. [import]uid: 85686 topic_id: 14209 reply_id: 52404[/import]

Actually, one question. When does incrementTimer get set to true again? It seems like after the first full day, the gameTime increment would stop. [import]uid: 85686 topic_id: 14209 reply_id: 52405[/import]

You would set this to true again on say a pop up saying “Day 1 complete” or you could simply reset it under gameTime = 0

:slight_smile: [import]uid: 84637 topic_id: 14209 reply_id: 52406[/import]