Reducing Animal Feed in Real Time with a Timer

I am having issues attempting something, reducing animal “feed” in real time when the app is closed.

Right now I have this:

 

local feedAmount = 100;

Then, what I want to do is reduce the amount of feed by 10 units per hour. However for testing purposes I am reducing it by 10 per second. To do this, I used a timer and repeat it every 10 seconds, thus reducing the feed amount.

local function reduceFeed(); if feedAmount == 100 then; feedAmount = feedAmount - 10; elseif feedAmount == 0 then; end; end; timer.performWithDelay( 1000, reduceFeed, [-1] ); 

However, I don’t know how to attempt this while the app is closed. I have tried several things with 

os.time 

but cannot find a solution. If I was steered in the correct path as far as how I am to go about this, it would certainly help. Running from a server is not an option.

Thank you for your assistance.

timer.performWithDelay wont be running while your app is closed like you have discovered.

I think the best thing is just to store how much feed user aquired and at what time it was aquired and then calculate from there every time you need that information.

So first thing is first is some method of storing that information on the device. Maybe GG Data from here: https://github.com/GlitchGames

Store on the device:

feedAmount = 1000

feedAquiredTime = 1234556 – use os.time()

Every time user gets more feed store the os.time() in FeedAquiredTime and feed amount in feedAmount. 

When you want to know how much feed the user has, calculate from that point. So do not use any timers at all and only update the file when user gets more feed.

[lua]local timeSinceLastFeedAquiring = os.time() - feedAquiredTime – In seconds

local amountOfFeedEaten = (timeSinceLastFeedAquiring / 10) * 10 – 10 units every 10 seconds

local currenFeedStatus = feedAmount - amountOfFeedEaten[/lua]

Something like that…

Thank you very much, this certainly does make sense! You’ve explained it very well, thank you.

timer.performWithDelay wont be running while your app is closed like you have discovered.

I think the best thing is just to store how much feed user aquired and at what time it was aquired and then calculate from there every time you need that information.

So first thing is first is some method of storing that information on the device. Maybe GG Data from here: https://github.com/GlitchGames

Store on the device:

feedAmount = 1000

feedAquiredTime = 1234556 – use os.time()

Every time user gets more feed store the os.time() in FeedAquiredTime and feed amount in feedAmount. 

When you want to know how much feed the user has, calculate from that point. So do not use any timers at all and only update the file when user gets more feed.

[lua]local timeSinceLastFeedAquiring = os.time() - feedAquiredTime – In seconds

local amountOfFeedEaten = (timeSinceLastFeedAquiring / 10) * 10 – 10 units every 10 seconds

local currenFeedStatus = feedAmount - amountOfFeedEaten[/lua]

Something like that…

Thank you very much, this certainly does make sense! You’ve explained it very well, thank you.