I want to make a cooldown timer and to working when the game is closed, can be possible?
So basically values from the current game instance stored on exit and retrieved on start?
Then you just have to write the cooldown time to the PlayerPrefs and read it out when the player continues where he was last.
If you do not want the player to cheat you will have to host a server which writes the cooldown time into a database for example. This is similar to MMOs, in which the game world co-exists on a server while players connect and disconnect and keep their profile-related values (cooldowns on their characters, items and so on).
To elaborate, you will essentially be storing the time left in a cooldown, along with a current timestamp. When the game starts up, you will read those values, and either subtract the difference between the current time with the timestamp from the remaining cooldown. If itās less than zero then you know the cooldown is finished, otherwise you can update the cooldown to the new remaining time.
You could also store the āfuture timestampā for when the cooldown will complete, and compare that with the current time on startup.
You can use the DateTime and TimeSpan classes to accomplish this.
Thanks for adding this information, that makes it more clear.
@ARares Maybe you have already discovered this by yourself but this makes the cooldowns cheatable. A player could cast a powerful spell with a long time to cool down, exit his game and turn the time a few minutes back and snap, the cooldown is reset. The PlayerPrefs are just a key in the registry and easily editable, and not even this would be needed if the timestamp is stored as well.
All this is why I told you about servers which keep the cooldown values to make them inaccessible for the players.
i donāt know how to make the cooldown to work when the game is closed.
There are replies above that answer this question. If you have problems understanding us, use a translator or ask us to simplify sentences that you cannot comprehend.
Iāll try and walk you through the logic here:
Keep in mind that ācurrent timeā is the value from DateTime.Now.
You start the cooldown when the game is running, say it is 10 minutes long. You calculate (ācurrent timeā + 10 minutes), Iāll call this āfutureTimeā, and save into local storage (PlayerPrefs).
You close the game. You wait 5 minutes, and open the game again.
You load āfutureTimeā from PlayerPrefs, and subtract the ācurrent timeā from it. That will result in 5 minutes, because āfutureTimeā is still 5 minutes away. So you can update the remaining time displayed to that value.
You close the game again, and wait 6 more minutes.
You open the game again, load āfutureTimeā, and subtract the current time from it. That will now result in -1 minutes, because you waited a total of 11 minutes, and the cooldown was only 10 minutes. Since that value is less than zero, you know the cooldown has finished, and can update the game accordingly.
Here is an example of some code that can do this stuff. It is up to you to figure out how to apply this to your game.
public class CooldownExample : MonoBehaviour
{
public enum CooldownName
{
A_Cooldown,
B_Cooldown,
C_Cooldown,
}
// call this while the game is running to start a cooldown
public void StartCooldown(CooldownName cooldownName, int days, int hours, int minutes, int seconds)
{
// get the time in the future that this cooldown will finish
DateTime futureTime = DateTime.Now + new TimeSpan(days, hours, minutes, seconds);
// convert the time into "ticks", the smallest time unit
long ticks = futureTime.Ticks;
// save the ticks as a string in local storage
PlayerPrefs.SetString(cooldownName.ToString(), ticks.ToString());
}
public void StopCooldown(CooldownName cooldownName) {
// remove the cooldown from local storage
PlayerPrefs.DeleteKey(cooldownName.ToString());
}
// call this when the game starts for all your cooldowns
public void UpdateCooldown(CooldownName cooldownName)
{
// get the cooldown string from local storage
string cooldownString = PlayerPrefs.GetString(cooldownName.ToString());
// if it exists
if(!string.IsNullOrEmpty(cooldownString)) {
// get the ticks from the string
long ticks = long.Parse(cooldownString);
// calculate the time remaining in the cooldown
TimeSpan timeRemaining = new DateTime(ticks) - DateTime.Now;
// check if there is time left in the cooldown
if(timeRemaining.Ticks > 0)
{
// cooldown has time remaining
int days = timeRemaining.Days;
int hours = timeRemaining.Hours;
int mins = timeRemaining.Minutes;
int seconds = timeRemaining.Seconds;
//
// do something here to update game for this cooldown
//
}
else
{
// cooldown is done
StopCooldown(cooldownName);
//
// do something here to update game for this cooldown
//
}
}
}
// save the local storage to disk when the game closes
private void OnApplicationQuit()
{
PlayerPrefs.Save();
}
}