Timers independant of the game i.e set timers

Timers independant of the game i.e set timers, I’ve been looking everywhere for a tutorial on how to use timers like candy crush where it takes the time from the moment you lose the heart untill 30 minutes then you get it back even if you close the game or something but I can’t find any clear ones help?

System.DateTime.Now; //will give you the current time
System.DateTime lastTimeSaved; // store the last time an action happened.(all hearts gone)

//Then what you want to so to get 30 minutes you have to add it to the last time an action happened.(all hearts gone)
System.DateTime OneHeartReplenished = lastTimeSaved.AddMinutes(30);

// Finally in your update script you will poll constantly if OneHeartReplenished is due

If(System.DateTime.Now>OneHeartReplenished)
{
  //generate new heart
}

If I want it to work even when you close down the game, I would make use of the DateTime functions.

(See DateTime documentation for more information)

First you need to store the “starting” date for when I want the timer to be activated:

DateTime timeAtActivation = DateTime.Now;

Now, you need to store this value somehow, either in a file, server or in the registry where you can acces it at any time from the game, even though the game has been turned closed in between game sessions.

So when you want to calculate how much time that has passed since it was activated, you ned to compare the current time with the time when the timer was activated. Something like so:

// Get your stored activation time first. And then the current time. 
DateTime currentTime = DateTime.Now;

TimeSpan TimeSpanValue = currentTime.Subtract(TheStoredActivatedTimeValue);
double elapsedTimeInMins = TimeSpanValue.TotalMinutes;

if(elapsedTimeInMins >= 30.0d)
   HasTimerReachedItsTimeLimit = true;

The TimeSpan variable can return different values. So take a look at the TimeSpan documentation for more information.

Good luck!

my timer keeps counting but I can’t find out how to set another timer for the next death and when i die it continually counts down the lives which is useless for a game

public class PlayerController : MonoBehaviour

{

//Character Motor Variables
Transform _charBottom;
float _moveSpeed = 10f;
bool _onGround;
bool _canDoAction;
bool _canJump;
float _jumpForce = 900f;
BoxCollider2D _collider;
SpriteRenderer _renderer;

//Lives System Variables
bool _gameOver = false;
int playerHeartCount = 5;
public bool livesinfinte = false;
public bool dead = false;
DateTime oldDate;
DateTime currentDate;
int minutes;

void Start() 
{
	//How player will get infinte lives if they pay for it
	if (livesinfinte == true) {
		Debug.Log ("Unlimited Lives");
	}
	//dead = true;

}

void Awake ()
{
	_charBottom = transform.FindChild ("charBottom"); 
	_collider = transform.FindChild ("collider").GetComponent<BoxCollider2D> ();
	_renderer = transform.FindChild ("spriteRenderer").GetComponent<SpriteRenderer> ();
}

// Update is called once per frame
void Update ()
{
	_onGround = Physics2D.Linecast (transform.position, _charBottom.position, 1 << LayerMask.NameToLayer ("Ground")); //checks ground

	if (!Overwatch.OW.IsPaused)
	{
		if (Input.GetKeyDown (KeyCode.Space))
		{
			if (_onGround) // change for double jump
			{
				_canJump = true;
			}
    
		}
		if (Input.GetKeyDown (KeyCode.LeftControl))
		{
			if (_onGround)
			{
				_canDoAction = true;
			}
		}
		else
		{
			_canDoAction = false;
		}
	}

	transform.position = new Vector3 (-8, transform.position.y, transform.position.z);

	currentDate = System.DateTime.Now;
	minutes = currentDate.Minute - oldDate.Minute;
	if (dead == true)
	{
		OnDeath();
	}
	if (minutes >= 30)
	{
		playerHeartCount++;

	}

}

void FixedUpdate ()
{
	if (_canJump)
	{
		jump ();
	}

}

void jump ()
{
	//modify for multi jump
	GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, _jumpForce));
	_canJump = false;
}

public void KillPlayer ()
{
	dead = true;
	//Application.LoadLevel (Application.loadedLevel);
}

public float MoveSpeed
{
	get { return _moveSpeed;}
	set { _moveSpeed = value;}
}

public bool CanDoAction
{
	get { return _canDoAction;}
}

public BoxCollider2D Collider
{
	get { return _collider;}
}

public SpriteRenderer Renderer
{
	get { return _renderer;}
}

void OnGUI() 
{ 
	GUI.Label (new Rect (10, 10, 100, 20), oldDate.ToString ());
	GUI.Label (new Rect (10, 20, 100, 20), currentDate.ToString ());
	GUI.Label (new Rect (10, 30, 100, 20), minutes.ToString ());
	GUI.Label (new Rect (10, 40, 100, 20), playerHeartCount.ToString ());
	if (_gameOver == true) {
		GUI.Label (new Rect (0, 0, 100, 20), "Game Over!");
		Overwatch.OW.IsPaused = true;
		GUI.Box(new Rect(600,200,200,190), "Respawn Menu");
		if(GUI.Button(new Rect(610,220,180,40), "Main Menu")) 
		{
			Application.LoadLevel (Application.loadedLevel);
		}
		if(GUI.Button(new Rect(610,260,180,40), "Retry")) 
		{
			Application.LoadLevel (Application.loadedLevel);
		}
	}
}
void OnDeath()
{
	oldDate = System.DateTime.Now;
	//will respawn here if lives available
	playerHeartCount -= 1;
	dead = false;
	_gameOver = true;

}

}