- Home /
How to measure the real time differential between application runs
Ok, i'm making a game like "My Talking Angela". I have all the sliders (happy, hunger, sleep...) and the values decrease when my game is open. But when i close or just go to "home" in my android, or sliders values reset or just stop decreasing.
How i can put to decrease even when i close the game? I tried this, based on the subtraction of the time that has passed, but doesn't work:
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
public class calcular_tempo : MonoBehaviour {
DateTime oldDate;
DateTime currentDate;
float seconds;
public Slider BarraFelicidade;
void Start()
{
currentDate = System.DateTime.Now;
seconds = currentDate.Second - oldDate.Second;
if(seconds/1 > BarraFelicidade.value) {
BarraFelicidade.value--;
}
}
void OnApplicationQuit ()
{
BarraFelicidade.value = BarraFelicidade.value;
seconds = 0;
oldDate = System.DateTime.Now;
PlayerPrefs.Save();
}
}
$$anonymous$$easuring the time that has passed since the application closed is the appropriate method. Things may get screwy if the user changes time zones or messes with their system time, but I wouldn't worry about that.
Ensure you're actually saving the oldDate values to PlayerPrefs - it doesn't look like you are, unless there's new shorthand I don't know about (?) or you're doing it somewhere else.
Have a look at the documentation for PlayerPrefs and the $$anonymous$$SDN docs for DateTime to ensure your logic is sound.
I'm afraid it won't be possible to help further without seeing your new code. Your code for this process should not be too big to post here; if it's more than a few lines, you're doing something in an inefficient way.
I recommend some research to see if anyone else has issues working with DateTime or PlayerPrefs on Android.
@blue-legs you can post your code to somewhere like pastebin and post that link here.
I'm not sure if OnQuit gets called always. If the app gets closed so that user puts the app to background first, it might not. Better use OnApplicationPaused
Just a few things to make your code small and clear.
Rather than breaking apart DateTime into day, month, hours, $$anonymous$$utes, seconds etc just store your current datetime as string. You can convert DateTime to string using DateTime.ToString $$anonymous$$ethod (String)
You can retrieve this string and convert it to DateTime again using How to: Convert a String to a DateTime (C# Program$$anonymous$$g Guide)
Convert your retrieved and current DateTime string to Linux Time and subtract them to get values in seconds. How to convert To and From DateTime to Linux Time.
Your answer
Follow this Question
Related Questions
Webplayer - action on leave page 1 Answer
Saving highscore with PlayerPrefs 1 Answer
Some questions that needs answers. 1 Answer
How to record the real playing time? 1 Answer
A Few Questions :D 3 Answers