- Home /
how to know the time passed since the player closed the game?
Hello
i wanna make a game for mobile devices that has some time dependent things.
for example to kill this monster you have to wait 30 minutes.
how can i do it in a way so the player cant cheat and change the iphone time?
You would need to draw on an external source of time, usually a server.
I think what @meat5000 means is record the time when you want to start the wait then, when the game opens again you can check the time again.
Or if you just want to record 30 $$anonymous$$utes of actual game time, as in when the game's open, then set a float to = 1800 when the 30 $$anonymous$$utes starts and reduce by Time.deltaTime each frame until you get back to 0.
Either way save what the variables are when you exit the game and load them again when it starts.
aha, so i save the the float in PlayerPrefs when he quit the game, and call it back when opens the game
so i use: OnApplicationQuit() but does that work on android and ios?
There is no way to do that offline as far as I know.
The only options I know of are connect to a server that definitely has the correct time, to let them cheat, or to make it so they have to be playing the entire 30 $$anonymous$$utes.
Answer by Firedan1176 · Jan 29, 2015 at 03:44 AM
You could use the function OnApplicationQuit() to save the time into the player's prefs folder (possibly as a float). When the game is started up again, you can take the current time and subtract the previous time.
Example: Exited previously at 10:00.00 PM, 1-28-15 > 20:00.00. You could then convert that to a float (from years to seconds) such as: 152801100000. If you logged back on at 8:00.00 AM 1-29-15 (next morning), it would be 152901080000. Now the only problem is if you forget to add an extra 0 (8 would be 08) for double-digits. Then you could subtract the prev. from the current, convert it to a string, parse/split it, then read that as time from last logged on. Alternatively, you could have it formatted as a string with dashes in between each "type" (days/months/seconds). It would then be easier to parse with String.Split since you could pass it the dash.
If you need more explanation, I'd be glad to provide.
The problem is not how to save the value but how to prevent cheating.
There is no way offline to prevent cheating.
But you can screw the cheating player in some way. Some players (including me) may change the clock, get in the game, collect the extra and then get out and set the clock back before returning in the game. Those are the players that want to cheat while keeping the device on time. While returning, you can check if the 30 $$anonymous$$ have passed and would we be before the recorded time. Then you fine them by removing a large amount of consumables. Angry Birds Transformers does that for instance.
Well there's really no way to prevent cheating. So unless you use some way of storing it, there's really no way to prevent cheating unless using encryption. But even that is possible to break. If your game can connect to the internet, then you could use that as an advantage. Ping some server to save the Server's time, and ping it again the next time they log on. They cannot change the server's time.
Answer by Naphier · Jan 29, 2015 at 10:32 AM
public static double DateTimeToUnixTimestamp(DateTime dateTime)
{
return (dateTime - new DateTime(1970, 1, 1)).TotalSeconds;
}
Use DateTime.UtcNow as the parameter then you have a nice UTC unix timestamp to work from. Store that in an encrypted file. You could do this every minute with little overhead.
fafase has the right idea on punishing cheaters.
Your answer
![](https://koobas.hobune.stream/wayback/20220613181815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Game will not load scene on MOBILE, but work fine on PC 0 Answers
How to open mobile file browsers 1 Answer
My assets size is 12MB but the game size is 66MB on iphone.. why? 3 Answers