How to auto trigger save method.

I have a save method to save the scene, It save the data only when user click save. Can the game trigger the save emthod said every 10 min, so the game can be save even user didnt click the save button?

You can use a Coroutine, and WaitForSeconds(600) to wait for 10 minutes.

2 Answers

2

To elaborate on my comment-

(C#) (I can do javascript as well, but I don’t usually use it)

public bool autoSave;
void Start()
{
    if(autosave)
     {
           StartCoroutine(AutoSaveTimer(600));
     }
}

 IEnumerator AutoSaveTimer(float time)
{
    //Save Game Here
    yield return new WaitForSeconds(time);
    if(autosave)
     {
           StartCoroutine(AutoSaveTimer(time));
     }
}

Short answer: Yes.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Long answer:

function Start() 
{
	SaveEvery10Minutes();
}

function SaveEvery10Minutes()
{
	while( true )
	{
		yield WaitForSeconds( 6000.0 );
	 	Save("Autosave.save");
	}
}

If you don’t want to use yield (for whatever reasons), use an old-school timer :

var mfSaveTimer: float;

function Start() 
{
	mfSaveTimer= 6000.0;
}

function Update()
{
	mfSaveTimer -= Time.deltaTime;
 	if( mfTimer <= 0 )
	{
    		Save("Autosave.save");
    		mfSaveTimer= 6000.0;
	 }   
}

6000 seconds is a really long time... I think you forgot how long a minute is. Coroutines are the best for this kind of thing- you would only use the timer option if you needed to know exactly how long it was until your autosave happened (I really can't think of a reason why you would do that).