From a3af44becb83d44e33d1463b89f749a15c83689e Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 19 May 2020 16:06:38 +0200 Subject: [PATCH] Fixed empty coroutines throwing a NRE Closes #3 --- Coroutine/ActiveCoroutine.cs | 1 - Coroutine/CoroutineHandler.cs | 2 ++ Test/Example.cs | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Coroutine/ActiveCoroutine.cs b/Coroutine/ActiveCoroutine.cs index 03a8812..cd69783 100644 --- a/Coroutine/ActiveCoroutine.cs +++ b/Coroutine/ActiveCoroutine.cs @@ -11,7 +11,6 @@ namespace Coroutine { internal ActiveCoroutine(IEnumerator enumerator) { this.enumerator = enumerator; - this.enumerator.MoveNext(); } public bool Cancel() { diff --git a/Coroutine/CoroutineHandler.cs b/Coroutine/CoroutineHandler.cs index 43ba3ca..6ca268e 100644 --- a/Coroutine/CoroutineHandler.cs +++ b/Coroutine/CoroutineHandler.cs @@ -9,6 +9,8 @@ namespace Coroutine { private static readonly List EventCoroutines = new List(); public static ActiveCoroutine Start(IEnumerator coroutine) { + if (!coroutine.MoveNext()) + return null; var inst = new ActiveCoroutine(coroutine); var type = inst.GetCurrentType(); if (type == WaitType.Tick) diff --git a/Test/Example.cs b/Test/Example.cs index 451f925..75b401c 100644 --- a/Test/Example.cs +++ b/Test/Example.cs @@ -12,6 +12,8 @@ namespace Test { var seconds = CoroutineHandler.Start(WaitSeconds()); CoroutineHandler.Start(PrintEvery10Seconds(seconds)); + CoroutineHandler.Start(EmptyCoroutine()); + CoroutineHandler.InvokeLater(new WaitSeconds(10), () => { Console.WriteLine("Raising test event"); CoroutineHandler.RaiseEvent(TestEvent); @@ -53,5 +55,9 @@ namespace Test { } } + private static IEnumerator EmptyCoroutine() { + yield break; + } + } } \ No newline at end of file