Pause a script completely

Hello,

I have a small modding system and want to make a wait option in it.
First, let me explain how the modding system works. I get the lines from a txt file, separate them into words, then test for a certain word using an if statement and do the action.

My problem is that I made it so that when I have a keyword wait, it will set a bool to false and will let other lines pass. This bool is also set automatically to true before a wait, it will go to false only when using wait, and after a WaitForSeconds, it will go back to true and let the commands/lines pass. The thing is that when it’s waiting with WaitForSeconds, the script lets the next lines pass, and the next line sees that the bool is set to false, so it will not activate, so I lose that line.

Here is the bit of the code:
bool ready;

void Start () {
ready = true;
}

(...)
if (param[0] == "wait") { //The first word of the line/command 
ready = false;
StartCoroutine(float.Parse(param[1]));
}
if (ready == true) {
(...)
}

IEnumerator Wait (float amount) {
yield return new WaitForSeconds(amount);
ready = true;
}

Note this code may be wrong because I had to make it again (I removed the original part as it wasn’t working.)

Thank you!

I believe the problem is your perception of how coroutines (and the command StartCoroutine) works. Basically, Startcoroutines happen, and then it runs the code immediately afterward, in a seperate thread. If you want the stuff in a coroutine to happen first, and for the game to “wait” for that to be over, you need to use

print("a!");
yield return StartCoroutine(yourWaitingFunctionName);
print("b!");

This will only print b after your coroutine is over. HOWEVER, this said, you cannot yield return like this unless you’re already in a coroutine. So your calling function must also be an ienumerator. Does that make sense?

I think you haven’t understood what a coroutine actually is or how it works ^^. If you don’t yield in a coroutine it will run through your code like a normal function. However you have the option to wait for certain things conditionally. Example:

IEnumerator Start()
{
    // Do your loading
    while (not done with the file)
    {
        // parse your params from the current "command"
        // [...]
        
        if (param[0] == "wait") {
            yield return new WaitForSeconds(float.Parse(param[1]));
        }
        else if(param[0] == "someOtherAwesomeCommand"){
            Debug.Log("Execute someOtherAwesomeCommand");
        }
        // [...]
    }
}

If there’s no “wait” command in the file it will just rush through your file without any delay and will finish even before the first Update is called. However if a wait command is encountered it will stop executing for the given time and then continue with the next command.

Ok, for clarification, here’s my LoadMod.cs

using UnityEngine;
using System.Collections;
using System.Xml;
using System;
using System.IO;

public class LoadMod : MonoBehaviour {
	//This script loads all mods that are found in A Game/mods

	string path; //Path to app
	public string[] files;
	ModParser parser;

	void Start () {
		parser = this.GetComponent<ModParser>();
		//Setup mods folder in app
		path = Application.dataPath;

		//If there is no mods directory, make one
		if (!Directory.Exists(path+"/mods")) {
			Directory.CreateDirectory(path+"/mods");
			
		}
		Load();
	} 

	public void Load() {

		//Set all mods files in one array
		files = Directory.GetFiles(path + "/mods", "*.txt");
		Debug.Log ("Mods Installed:");

		for (int i = 0; i < files.Length; i++){
			string file = files*;*
  •  	GetComponent<ModParser>().x = files.Length;*
    
  •  	//Debug.Log (file); //Debugs all mods*
    
  •  	string[] lines = System.IO.File.ReadAllLines(file);*
    
  •  	parser.Parse(lines);*
    
  •  }*
    
  • }*
    }
    Then there is ModParser.cs (which I renamed, it used to be Mod.cs). It’s a simplified version as there are more if statements for param[0] but they would make this post too big
    using UnityEngine;
    using System.Collections;
    using UnityStandardAssets.Characters.FirstPerson;
    using System.Collections.Generic;
    using System.Linq;
    using UnityEngine.UI;

public class ModParser : MonoBehaviour
{

  • //This script takes the strings (mods) from LoadMod and translates them into a Unity based system.*

  • //This is basically the odding system itself, LoadMod is just to load the mods, this script makes all the processing and modding*

  • public int x = 0; // Used for information*

  • bool ready;*

  • //This will translate the string into the unity system*

  • public void Parse (string modLines)*

  • {*

  •  int i = 0;*
    
  •  lineNumber = 1;*
    
  •  while (i < modLines.Length) {//Makes each line a unique string*
    

_ Use (modLines );_
* lineNumber++;*
* i++;*
* }*
* }*

* public void Use (string modLine){*

* string[] param = modLine.Split (" " [0]);*

* if (param[0] == “wait”) {*
* ready = false;*
* StartCoroutine(Wait(float.Parse(param[1])));*
* }*

* if (param [0] == “display” && ready == true) { //Display text in chat or title*

* if (param [1] == “chat”) {*
* string finalOutput = “”;*
* int i = 0;*
* while (i < param.Length) {*
* if (i != 0 && i != 1) {*
_ finalOutput = finalOutput + " " + param ;
* }
i++;
}
GameObject.FindGameObjectWithTag (“Player”).GetComponent ().output = finalOutput; //Commands is basically the Chat script, output is the text that appears in the chat*
* }else {
Error (1);
}
}
else {
Error (1);//This will send an error from another method*
* }*_

* }*

* IEnumerator Wait (float time) {*
* yield return new WaitForSeconds(time);*
* ready = true;*
* }*
}