NickNDS

IEnumerator Example / Template

Nov 27th, 2020 (edited)
637
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public Program()
  2. {
  3.     Runtime.UpdateFrequency = UpdateFrequency.Update1;
  4. }
  5.  
  6. public void Main(string argument, UpdateType updateSource)
  7. {
  8.     RunScriptState();
  9. }
  10.  
  11. public void RunScriptState()
  12. {
  13.     if (scriptState == null) scriptState = ScriptState();
  14.  
  15.     bool moreWork = false;
  16.  
  17.     try
  18.     {
  19.         moreWork = scriptState.MoveNext();
  20.     }
  21.     catch { Echo("Error caught running script"); }
  22.  
  23.     if (!moreWork)
  24.     {
  25.         try
  26.         {
  27.             scriptState.Dispose();
  28.         }
  29.         catch { }
  30.         try
  31.         {
  32.             scriptState = null;
  33.         }
  34.         catch { }
  35.     }
  36. }
  37.  
  38. public IEnumerator<bool> scriptState;
  39.        
  40. public IEnumerator<bool> ScriptState()
  41. {
  42.     //Run long tasks in here
  43.     //Pause with 'yield return true;'
  44.     yield return false;
  45. }
RAW Paste Data