Trying to create a timer that runs outside the Game
Hi all, I am trying to create a timer that runs outside the game. Meaning that even when the application is closed the timer will recharge , in this case "fuel". All it does now is to recharge but once(not until the fuel is maximum) . If you could help me that will be awesome ! Cheers! public ulong lastchestopen; public float msToWait; public bool StartReloadFuel; public string r; public bool Test;
public int maxFuel;
public int fuel;
public int cur_fuel;
private void Awake()
{
DontDestroyOnLoad(this);
if (FindObjectsOfType(GetType()).Length > 1)
{
Destroy(gameObject);
}
}
private void Start()
{
lastchestopen = ulong.Parse(PlayerPrefs.GetString("LastChestOpe"));
fuel = PlayerPrefs.GetInt("Fuel");
if (!IsChestReady())
{
StartReloadFuel = false;
Test = false ;
}
}
private void Update()
{
fuel = Mathf.Clamp(fuel, 0, maxFuel);
if (fuel < maxFuel && Test )
{
Invoke("ChestClick" ,1);
Test = false;
}
PlayerPrefs.SetInt("MaxFuel", maxFuel);
PlayerPrefs.SetInt("Fuel", fuel);
if (fuel >= maxFuel)
{
StartReloadFuel = false;
Test = false;
}
if (!StartReloadFuel)
{
if (IsChestReady())
{
StartReloadFuel = true;
return;
}
//set timer
ulong diff = ((ulong)DateTime.Now.Ticks - lastchestopen);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float secondsLeft = (float)(msToWait - m) / 1000.0f;
r = "";
secondsLeft -= ((int)secondsLeft / 3600) * 3600;
//minutes
r += ((int)secondsLeft / 60).ToString("00") + "m ";
//seconds
r += ((int)secondsLeft % 60).ToString("00") + "s "; ;
//chestTimer.text = r;
}
PlayerPrefs.SetString("r", r);
}
IEnumerator TestingFuel()
{
while (fuel < maxFuel)
{
yield return new WaitForSeconds(1);
}
}
public void ChestClick()
{
lastchestopen = (ulong)DateTime.Now.Ticks;
PlayerPrefs.SetString("LastChestOpe", DateTime.Now.Ticks.ToString());
PlayerPrefs.SetInt("Fuel", fuel);
StartReloadFuel = false;
}
private bool IsChestReady()
{
ulong diff = ((ulong)DateTime.Now.Ticks - lastchestopen);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float secondsLeft = (float)(msToWait - m) / 1000.0f;
if (secondsLeft < 0)
{
if (fuel < maxFuel)
{
Test = true;
AddFuel(2);
fuel = cur_fuel;
}
return true;
}
return false;
}
public void AddFuel(int newFuel)
{
cur_fuel = fuel + newFuel;
}
public void SubFuel(int newFuel)
{
fuel = cur_fuel - newFuel;
}
Answer by tormentoarmagedoom · Feb 02, 2019 at 08:36 PM
Good day.
I will not read your code.
If you pretend something that "works" while is not running, the only thing you can take as a reference is the actual real time. This means register Year, month, dai, hour and minute of the moment it commence to "count".
Then compare to the actual Year, month, dai, hour and minute, so oyu know how many minutes passed from the time it was commenced, so you can calculate how many fuel it have now.
System.DateTime.Now.Hour
Bye.
Your answer
Follow this Question
Related Questions
How can i make countdown tick-tock sound ? 0 Answers
Farming game Time management 0 Answers
How to get a countdown timer with values entered by the player 0 Answers
How do I stop a scene from switching from switch before the Countdown timer reaches zero 2 Answers
Creating a timer that starts when my 3rd scene is loaded 1 Answer