Skip to main content
added 317 characters in body
Source Link
user441521
  • 792
  • 2
  • 7
  • 16

I'm using Unity but creating my own process that has it's own coroutine process. So I can't use StartCoroutine() but I'm interested in how that method works since it's easier to use than me getting the IEnumerator and stepping over it inside my function that's already called as a coroutine.

How can I create some kind of StartCoroutine() type function for users to use instead of having them step over the IEnumerator manually. So for example below I step over the IEnumerator Utilities.Wait() function manually. How could I just call that and have it do that for me and wait the specific amount of time?

public IEnumerator reduceHealth(dynamic args)
    {
        int result = Health - args.reductionValue;
        Debug.Log("Before health: " + Health);
        Debug.Log("Reducing health");

        // reduce health over time
        while (Health > result)
        {
            Health -= 1;
            Debug.Log("Health = " + Health);

            // how can I simplify this for users of this library
            var co = Utilities.Wait(2000);
            while (co.MoveNext())
                yield return null;
        }

        // snap to end result
        Health = result;

        // tell everyone we've been hurt!
        //onHurt.Raise(new { value = health });
    }


public class Utilities
{

    public static IEnumerator Wait(int ms)
    {
        var watch = System.Diagnostics.Stopwatch.StartNew();

        long lastTime = watch.ElapsedMilliseconds;
        while (watch.ElapsedMilliseconds <= lastTime + ms)
        {
            yield return null;
        }

        watch.Stop();
    }
}

I'm using Unity but creating my own process that has it's own coroutine process. So I can't use StartCoroutine() but I'm interested in how that method works since it's easier to use than me getting the IEnumerator and stepping over it inside my function that's already called as a coroutine.

public IEnumerator reduceHealth(dynamic args)
    {
        int result = Health - args.reductionValue;
        Debug.Log("Before health: " + Health);
        Debug.Log("Reducing health");

        // reduce health over time
        while (Health > result)
        {
            Health -= 1;
            Debug.Log("Health = " + Health);

            // how can I simplify this for users of this library
            var co = Utilities.Wait(2000);
            while (co.MoveNext())
                yield return null;
        }

        // snap to end result
        Health = result;

        // tell everyone we've been hurt!
        //onHurt.Raise(new { value = health });
    }


public class Utilities
{

    public static IEnumerator Wait(int ms)
    {
        var watch = System.Diagnostics.Stopwatch.StartNew();

        long lastTime = watch.ElapsedMilliseconds;
        while (watch.ElapsedMilliseconds <= lastTime + ms)
        {
            yield return null;
        }

        watch.Stop();
    }
}

I'm using Unity but creating my own process that has it's own coroutine process. So I can't use StartCoroutine() but I'm interested in how that method works since it's easier to use than me getting the IEnumerator and stepping over it inside my function that's already called as a coroutine.

How can I create some kind of StartCoroutine() type function for users to use instead of having them step over the IEnumerator manually. So for example below I step over the IEnumerator Utilities.Wait() function manually. How could I just call that and have it do that for me and wait the specific amount of time?

public IEnumerator reduceHealth(dynamic args)
    {
        int result = Health - args.reductionValue;
        Debug.Log("Before health: " + Health);
        Debug.Log("Reducing health");

        // reduce health over time
        while (Health > result)
        {
            Health -= 1;
            Debug.Log("Health = " + Health);

            // how can I simplify this for users of this library
            var co = Utilities.Wait(2000);
            while (co.MoveNext())
                yield return null;
        }

        // snap to end result
        Health = result;

        // tell everyone we've been hurt!
        //onHurt.Raise(new { value = health });
    }


public class Utilities
{

    public static IEnumerator Wait(int ms)
    {
        var watch = System.Diagnostics.Stopwatch.StartNew();

        long lastTime = watch.ElapsedMilliseconds;
        while (watch.ElapsedMilliseconds <= lastTime + ms)
        {
            yield return null;
        }

        watch.Stop();
    }
}
added 417 characters in body
Source Link
user441521
  • 792
  • 2
  • 7
  • 16

I'm using Unity but creating my own process that has it's own coroutine process. So I can't use StartCoroutine() but I'm interested in how that method works since it's easier to use than me getting the IEnumerator and stepping over it inside my function that's already called as a coroutine.

public IEnumerator reduceHealth(dynamic args)
    {
        int result = Health - args.reductionValue;
        Debug.Log("Before health: " + Health);
        Debug.Log("Reducing health");

        // reduce health over time
        while (Health > result)
        {
            Health -= 1;
            Debug.Log("Health = " + Health);

            // how can I simplify this for users of this library
            var co = Utilities.Wait(2000);
            while (co.MoveNext())
                yield return null;
        }

        // snap to end result
        Health = result;

        // tell everyone we've been hurt!
        //onHurt.Raise(new { value = health });
    } 


public class Utilities
{

    public static IEnumerator Wait(int ms)
    {
        var watch = System.Diagnostics.Stopwatch.StartNew();

        long lastTime = watch.ElapsedMilliseconds;
        while (watch.ElapsedMilliseconds <= lastTime + ms)
        {
            yield return null;
        }

        watch.Stop();
    }
}

I'm using Unity but creating my own process that has it's own coroutine process. So I can't use StartCoroutine() but I'm interested in how that method works since it's easier to use than me getting the IEnumerator and stepping over it inside my function that's already called as a coroutine.

public IEnumerator reduceHealth(dynamic args)
    {
        int result = Health - args.reductionValue;
        Debug.Log("Before health: " + Health);
        Debug.Log("Reducing health");

        // reduce health over time
        while (Health > result)
        {
            Health -= 1;
            Debug.Log("Health = " + Health);

            // how can I simplify this for users of this library
            var co = Utilities.Wait(2000);
            while (co.MoveNext())
                yield return null;
        }

        // snap to end result
        Health = result;

        // tell everyone we've been hurt!
        //onHurt.Raise(new { value = health });
    }

I'm using Unity but creating my own process that has it's own coroutine process. So I can't use StartCoroutine() but I'm interested in how that method works since it's easier to use than me getting the IEnumerator and stepping over it inside my function that's already called as a coroutine.

public IEnumerator reduceHealth(dynamic args)
    {
        int result = Health - args.reductionValue;
        Debug.Log("Before health: " + Health);
        Debug.Log("Reducing health");

        // reduce health over time
        while (Health > result)
        {
            Health -= 1;
            Debug.Log("Health = " + Health);

            // how can I simplify this for users of this library
            var co = Utilities.Wait(2000);
            while (co.MoveNext())
                yield return null;
        }

        // snap to end result
        Health = result;

        // tell everyone we've been hurt!
        //onHurt.Raise(new { value = health });
    } 


public class Utilities
{

    public static IEnumerator Wait(int ms)
    {
        var watch = System.Diagnostics.Stopwatch.StartNew();

        long lastTime = watch.ElapsedMilliseconds;
        while (watch.ElapsedMilliseconds <= lastTime + ms)
        {
            yield return null;
        }

        watch.Stop();
    }
}
Source Link
user441521
  • 792
  • 2
  • 7
  • 16

My own coroutines waiting until finished from within a coroutine in C#

I'm using Unity but creating my own process that has it's own coroutine process. So I can't use StartCoroutine() but I'm interested in how that method works since it's easier to use than me getting the IEnumerator and stepping over it inside my function that's already called as a coroutine.

public IEnumerator reduceHealth(dynamic args)
    {
        int result = Health - args.reductionValue;
        Debug.Log("Before health: " + Health);
        Debug.Log("Reducing health");

        // reduce health over time
        while (Health > result)
        {
            Health -= 1;
            Debug.Log("Health = " + Health);

            // how can I simplify this for users of this library
            var co = Utilities.Wait(2000);
            while (co.MoveNext())
                yield return null;
        }

        // snap to end result
        Health = result;

        // tell everyone we've been hurt!
        //onHurt.Raise(new { value = health });
    }