- Home /
I want to create a coroutine that will run a series of functions when a set variable reaches its value. What would be the best way to do this?
So, like the title said, I want to create some kind of IEnumerator that will wait until my first parameter's value changes to my second argument, the goal value, and run the third argument which will be a delegate (called ToRun, has no arguments).
I had created something like this:
public IEnumerator WaitForValue (float Parameter, float Value, ToRun Queue)
{
if (Queue != null)
{
yield return new WaitUntil(() => Mathf.Abs(Parameter - Value) <= 0.02f);
Queue();
}
yield return null;
}
but it only works for certain objects (for some reason it works for color components, but doesn't for regular floats). Now, maybe there is some other error that I do not see, but seeing as how values are passed in C#, floats are passed by value, not reference, therefore my coroutine shouldn't work for floats since I'm passing just the value of the original float I'm looking at, but it doesn't get updated. Now, I don't know why my coroutine works for sure, but I also do not know why it doesn't work in some cases (I checked with debug log, event is not triggered even though I see the parameter reach the desired value). In fact, it works when my Value argument is 0f, but somehow doesn't when it is 1f (even though the 0.02f threshold is large enough to accept float mistakes which were -0.00093 in the most extreme cases).
So, I guess I'm doing this sub-optimally. I use this coroutine to order my transitions around, like some sort of a state machine controller. I reckon I will need this method and its overloads later in the game I'm making as well because I don't want to constantly run checks in Update.
So, does the best way (or at least a better way) to do this in C# exist since I can't force ref values for WaitUntil?