I am making a game in Lua - specifically Corona SDK - and I am stuck on a timed health bonus (the player would get a health bonus every 4 hours) and the second part is the player would get a free spin of a wheel type game that will give them the chance to win free items to use in the game.
The part that is really confusing me is how do I get the timed bonus to be accurate (ex. every 4 hours and once a day) to fire off the functions for the bonuses? I also want there to be a count down timer showing hours:minutes:seconds left before next available bonus can be collected. Is this impossible to do?
Here is some of my code so far...
function hourlyBonus()
local date = os.date( "*t" )
local currentHour = date.hour
lastHourlyBonusClaimedHour =
GameSave.lastHourlyBonusClaimedHour or date.hour
--account for the 24 hour clock
if currentHour > 12 then
currentHour = currentHour - 12
end
if lastHourlyBonusClaimedHour > 12 then
lastHourlyBonusClaimedHour = lastHourlyBonusClaimedHour - 12
end
if currentHour == (lastHourlyBonusClaimedHour + 4) then
lastHourlyBonusClaimedHour = currentHour
-- increase the bonus
print("New 4 hour bonus ThisHour is: " .. thisHourNum)
else
local hoursToWait = (4 - (currentHour - lastHourlyBonusClaimedHour))
--have to wait for hourly bonus
print("Have to wait: "
.. hoursToWait .. "hours, "
.. minutes .. "minutes, and "
.. second .. "seconds to collect hourly Bonus still!"
)
print("CurrentHour is:" .. currentHour)
print("LastHourlyBonusClaimedHour is :" .. lastHourlyBonusClaimedHour)
end
GameSave.lastHourlyBonusClaimedHour = lastHourlyBonusClaimedHour
GameSave:save()
end
If anyone has some sample code I could look at or show me how to do this I would greatly appreciate it!