Tracking and Updating Game Lives

In my game I am giving them 5 game lives.  When a game life is used, I want to replace the game life every thirty minutes.  I’ve tried various methods of trying to capture the game time and save to a table then retrieve the game times and replace the lives every thirty minutes and erase the game time for the life replaced.  The problem is, there are so many iterations the code just goes into a timing mode so it is obviously extremely non efficient.  I’ve been working on this for several days and now I am seeking guidance.  Please help me develop a way to track game lives used and then replace them every thirty minutes; I have to replace the one originally used first in time.

Thanks,

Lori

Hi lgalcott,

Have you tried loop? Try using array with if condition inside the loop.

Regrads,

Yes.   I tried several different kinds of loops.  I broke it into pieces.  I have a runtime event to determine which game life is earliest in time and then it goes to a local function to make the update.    Thank you for responding!   Have a great day.

I meant to clarify - this part of the code is working. 

This is the simplest form I could break this down into:

local maxNumberOfLives = 5 local timeBetweenLives = 30 \* 60 -- (30 mins \* 60 secs) local gameLifeTimes = {} --load/save these values from/to a json file between application sessions local gameLives = {0, 0, 0, 0, 0} --load/save these values from/to a json file between application sessions local function checkLives() local now = os.time() for i = 1, maxNumberOfLives do if gameLifeTimes[i] ~= nil then --check if enough time has passed between losing the life and now if now \>= gameLifeTimes[i] + timeBetweenLives then gameLives[i] = 1 gameLifeTimes[i] = nil -- save whenever values change mySaveFunction() end end end end local function loseALife() for i = 1, #gameLives do --check if the life can be lost (1) or has already been lost (0) if gameLives[i] \> 0 then --this life can be lost, so remove it and save the time it was lost gameLives[i] = 0 gameLifeTimes[i] = os.time() -- save whenever values change mySaveFunction() break --stop the loop end end end timer.performWithDelay( 1000, checkLives, 1)

Something like this will check once per second to see if a new life has been earned (and I’ve also shown a way to remove the lives).

If you haven’t already done so, you’ll need to find some function for saving data permanently between sessions for this to work.

Thank you!  I will try that tonight.  I appreciate the time and effort you put into the answer - it really helps newbie’s such as myself grasp a better understanding. 

Lori

Minor correction:

     timer.performWithDelay( 1000, checkLives, 1)

will only trigger once (the last argument).  To trigger it over and over again, use:

     timer.performWithDelay( 1000, checkLives, -1)

The other option would be to catch the enterFrame event … that should trigger 30 or 60 times per second … the only advantage would be if you’re already using enterFrame to run the game itself.  If you’re not, then the timer is probably better.

RESOLVED:  Thank you for all of the wonderful input and guidance.  The issue has been resolved and is working.  

Have a great day!

Hi lgalcott,

Have you tried loop? Try using array with if condition inside the loop.

Regrads,

Yes.   I tried several different kinds of loops.  I broke it into pieces.  I have a runtime event to determine which game life is earliest in time and then it goes to a local function to make the update.    Thank you for responding!   Have a great day.

I meant to clarify - this part of the code is working. 

This is the simplest form I could break this down into:

local maxNumberOfLives = 5 local timeBetweenLives = 30 \* 60 -- (30 mins \* 60 secs) local gameLifeTimes = {} --load/save these values from/to a json file between application sessions local gameLives = {0, 0, 0, 0, 0} --load/save these values from/to a json file between application sessions local function checkLives() local now = os.time() for i = 1, maxNumberOfLives do if gameLifeTimes[i] ~= nil then --check if enough time has passed between losing the life and now if now \>= gameLifeTimes[i] + timeBetweenLives then gameLives[i] = 1 gameLifeTimes[i] = nil -- save whenever values change mySaveFunction() end end end end local function loseALife() for i = 1, #gameLives do --check if the life can be lost (1) or has already been lost (0) if gameLives[i] \> 0 then --this life can be lost, so remove it and save the time it was lost gameLives[i] = 0 gameLifeTimes[i] = os.time() -- save whenever values change mySaveFunction() break --stop the loop end end end timer.performWithDelay( 1000, checkLives, 1)

Something like this will check once per second to see if a new life has been earned (and I’ve also shown a way to remove the lives).

If you haven’t already done so, you’ll need to find some function for saving data permanently between sessions for this to work.

Thank you!  I will try that tonight.  I appreciate the time and effort you put into the answer - it really helps newbie’s such as myself grasp a better understanding. 

Lori

Minor correction:

     timer.performWithDelay( 1000, checkLives, 1)

will only trigger once (the last argument).  To trigger it over and over again, use:

     timer.performWithDelay( 1000, checkLives, -1)

The other option would be to catch the enterFrame event … that should trigger 30 or 60 times per second … the only advantage would be if you’re already using enterFrame to run the game itself.  If you’re not, then the timer is probably better.

RESOLVED:  Thank you for all of the wonderful input and guidance.  The issue has been resolved and is working.  

Have a great day!