If you’re doing 5 hp/sec, you don’t want to save every sec most likely. Saving when pausing/quitting/app going to the background/those sorts of things is probably better. Or saving at certain intervals. A crash may have the player lose some “regen”, so intervals is fine. Every few mins should be ok, depending on how critical this regen is to your game mechanics.
I’m the type I would just simply stick the regen function in the Update override of your GameObject, but that’s just me. As for the saving, you could just simply save at certain intervals (like a 5 minute autosave) and when you do, you record both the current HP and the current HP Regen rate.
I want to make game it’s have time stamp function like hay day or clash of clan.
Can you suggest function for saving at certain intervals ? IEnumerator, StartCoroutine Or InvokeRepeating ?
float lastUpdated = -999f;
public float updateInterval = 10f;
void Update() {
if (Time.time > lastUpdated + updateInterval) {
lastUpdated = Time.time;
//save stuff here
}
}
This will be more durable to different situations than the other approaches. e.g. if this object is deactivated then activated later, this will resume working without a hitch, while a coroutine or InvokeRepeating would be killed.