"Decrease power when app not open or running"

Hey guys I was wondering how to make it so when the user is not in the game or have the app open then it will take away “points” or “power” from the users point value. I just want to make it so a variable gets subtracted when the player is “offline”

Thanks Tyler

What you will need to do is store the time that the app is closed. Then when the app is opened compare the current time to the stored time, and calculate how much “power” you need to deduct.

local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then local now = os.time() local exitTime = loadTheTimeFromSavedJsonFile() if exitTime == nil then exitTime = now end local timeBetweenSessions = os.difftime( now, exitTime ) local powerToDrainPerSecond = 1 local power = loadPowerFromSavedJsonFile() power = power - (powerToDrainPerSecond \* timeBetweenSessions) elseif event.type == "applicationExit" or event.type == "applicationSuspend" then local exitTime = os.time() saveTheTimeToJsonFile(exitTime) end end Runtime:addEventListener( "system", onSystemEvent )

In this example, I save the time when the user leaves the app, and also the current amount of power. There are plenty of examples about how to save data to json files or whatever method you fancy using, so I won’t explain them here.

Then when the app restarts, you find out how much time has passed, load the previous amount of power, and then adjust the amount of power based on the amount of time that has passed. I’ve kept it as simple as possible, and removed 1 power per second that the game was closed. Depending on how your game works you might want to only remove power for every minute/hour/etc that has passed, so you’d need to find out how many minutes/hours have passed by dividing the number of seconds.

Edit: I added a catch so that the first time the app is ever opened, it will just set the “exitTime” to be the same as “now”. That way the time difference will be 0s, and no power will be removed. It also means you avoid nil errors if the stored time does not exist.

Very cool! I was thinking about using server side logic for this, but this is so much better.

Server side would probably be better, because when it’s done locally as in my example the user can just put the device time forward/back to bypass your game logic. I’d be inclined to do roughly the same thing in the server side code though, and just keep track of when the user was last in the game.

You might need to do something a bit smarter though to account for the user’s internet connection cutting out, like periodically saving data to the server (see games like Clash of Clans for an example). If they have no connection then lock them out of the game.  

Since this is the newbie forum I thought that might be a little bit much to cover in my first post  :slight_smile:

What you will need to do is store the time that the app is closed. Then when the app is opened compare the current time to the stored time, and calculate how much “power” you need to deduct.

local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then local now = os.time() local exitTime = loadTheTimeFromSavedJsonFile() if exitTime == nil then exitTime = now end local timeBetweenSessions = os.difftime( now, exitTime ) local powerToDrainPerSecond = 1 local power = loadPowerFromSavedJsonFile() power = power - (powerToDrainPerSecond \* timeBetweenSessions) elseif event.type == "applicationExit" or event.type == "applicationSuspend" then local exitTime = os.time() saveTheTimeToJsonFile(exitTime) end end Runtime:addEventListener( "system", onSystemEvent )

In this example, I save the time when the user leaves the app, and also the current amount of power. There are plenty of examples about how to save data to json files or whatever method you fancy using, so I won’t explain them here.

Then when the app restarts, you find out how much time has passed, load the previous amount of power, and then adjust the amount of power based on the amount of time that has passed. I’ve kept it as simple as possible, and removed 1 power per second that the game was closed. Depending on how your game works you might want to only remove power for every minute/hour/etc that has passed, so you’d need to find out how many minutes/hours have passed by dividing the number of seconds.

Edit: I added a catch so that the first time the app is ever opened, it will just set the “exitTime” to be the same as “now”. That way the time difference will be 0s, and no power will be removed. It also means you avoid nil errors if the stored time does not exist.

Very cool! I was thinking about using server side logic for this, but this is so much better.

Server side would probably be better, because when it’s done locally as in my example the user can just put the device time forward/back to bypass your game logic. I’d be inclined to do roughly the same thing in the server side code though, and just keep track of when the user was last in the game.

You might need to do something a bit smarter though to account for the user’s internet connection cutting out, like periodically saving data to the server (see games like Clash of Clans for an example). If they have no connection then lock them out of the game.  

Since this is the newbie forum I thought that might be a little bit much to cover in my first post  :slight_smile: