Injustice 2 like Energy System

Hello fellow developers,

I am trying to implement an energy system same as in Injustice 2 where if user uses some (amount) energy to play a level the timer should start and after every cool down time it should award 1 energy to the user. Problem I am facing is I am not able to figure out a way to save new timestamp to same variable after every energy increment iteration. Any help/guidance on how to tackle this problem is appreciated. Thanks in advance.

PS: I haven’t worked with DateTime before, this is the first time.

Interesting question, this should help out:

int energy = 0;
var time = DateTime.Now;
time.AddMinutes(5);

There’s also other Add functions for seconds etc…

Check out the example here: DateTime.AddMinutes(Double) Method (System) | Microsoft Learn

Hi, Thanks for replying. I suppose you’re saying to use DateTime.Now.AddMinutes. It’s a good idea but I want to save new TimeStamp (DateTime.Now.Ticks) to playerprefs everytime 1 energy is added and use that timestamp to check if cool down has ended for new energy to add and repeat above steps. Saving TimeStamp to playerprefs is important.

I will also give AddMinutes a try and find a way to use it in my case.

Hello,

I’ve deleted my previous reply as it wouldn’t work due to Unity’s floating point system.

This should work:

string time = DateTime.Now.Ticks.ToString();
        PlayerPrefs.SetString("newtimetest", time);
        Debug.Log(time);
        decimal savedTime = decimal.Parse(PlayerPrefs.GetString("newtimetest"));
        Debug.Log("Saved: " + savedTime);

This explains why I used decimal to load the saved string:
https://stackoverflow.com/a/46385411

I’m saving as a string because PlayerPrefs has no SetDecimal function.

May I ask why is this in playerprefs and not your own class?

This is because my project is on Android and the energy system should also work when the game is off. If there’s other way to achieve this please let me know.

Thanks for the update, I’ll try that soon and let you know how it goes. :slight_smile:

Hi,
I tried this way, you can see the code. Is there any way I can repeat SetTimeStamp() and EnergyTimer() every energyRefillTime i.e. 10 seconds for now?

using System;
using UnityEngine;
using UnityEngine.UI;
public class EnergySystem : MonoBehaviour
{
    private Text energyTimer;
    private Text energyText;
   
    [SerializeField]
    private int maxEnergy = 10;
    [SerializeField]
    private int currentEnergy;
    [SerializeField]
    private float energyRefillTime;
    private ulong lastEnergyAdded;
    //private float timeLeft;
    private bool addEnergy;
    private void Start()
    {
        //lastEnergyAdded = ulong.Parse(PlayerPrefs.GetString("lastTime"));
        if (lastEnergyAdded.ToString() == null)
            lastEnergyAdded = ulong.Parse(PlayerPrefs.GetString("lastTime"));
        else
            if(currentEnergy < maxEnergy)
                    SetTimeStamp();
    }
    private void Update()
    {
        Debug.Log("Current Energy: " + currentEnergy);
    }
    private void SetTimeStamp()
    {
        lastEnergyAdded = (ulong)DateTime.Now.Ticks;
        PlayerPrefs.SetString("lastTime", lastEnergyAdded.ToString());
        lastEnergyAdded = ulong.Parse(PlayerPrefs.GetString("lastTime"));
        InvokeRepeating("EnergyTimer", 1.0f, 1.0f);
    }
    private void EnergyTimer()
    {       
        ulong difference = (ulong)DateTime.Now.Ticks - lastEnergyAdded;
        //Debug.Log(difference);
        ulong ms = difference / TimeSpan.TicksPerSecond;
        //Debug.Log(ms);
        float timeLeft = energyRefillTime - ms;
        Debug.Log(timeLeft);
        if(timeLeft == 0)
        {
            currentEnergy += 1;
        }
    }
}

}

Look into Coroutines, specifically WaitForSeconds: https://docs.unity3d.com/ScriptReference/WaitForSeconds.html

Also I’m really not sure why you’re using DateTime ticks, why not just the standard DateTime.Now?

Because I also plan to display the cooldown timer