I’d like to use this function for multiple booleans/animations. However, since this is a IEnumerator I won’t be able to use a ref parameter… Now I’ve looked into the Action method, but that doesn’t seem entirely right for my problem either. Anybody got any advice on how to solve this problem?
As @Kiwasi mentioned, you can pass in lambda functions as paramters which can effectionally pass in references. Here is an example of how you can “hack” references into Enumerators:
Hi, I hope it’s not too late to add to another question to this.
With the above example you are setting “memberValue” from the “boolToAssign” value. Is there a way to go the opposite way and have “memberValue” update something within the coroutine?
Basically I want to have an IEnumerator with a while loop that watches the current value of a variable.
I’m a bit stuck with this, but this seems really close to what I’m trying to do.
Thanks!
Hey thanks Kurt, that sounds right but there’s something I’m not getting Sorry!
How can you point to different bools for “Foobar” to reference?
If I do something like this -
I don’t think you can bridge through like that with a ref or out argument… you could pass the function in multiple layers though… perhaps there is a C# language weenie who can suggest a better construct?
EDIT: just realized you could make a little BoolBox class that contains the bool and then pass that around… other things would reference that bool via the class reference. Just tested and it works nicely.
using System.Collections;
using UnityEngine;
public class RoolTheBools : MonoBehaviour
{
public class BoolBox
{
public bool TheBool;
public BoolBox() { }
public BoolBox(bool x) { TheBool = x; }
}
IEnumerator Foobar(System.Func<BoolBox> GetSomeBoolBox)
{
while (true)
{
Debug.Log("Bool is " + GetSomeBoolBox().TheBool);
yield return null;
}
}
public void WaitForTest(BoolBox SomeBoolBox)
{
StartCoroutine(Foobar(() => { return SomeBoolBox; }));
}
BoolBox box;
private void Start()
{
box = new BoolBox();
WaitForTest(box);
}
private void Update()
{
box.TheBool = !box.TheBool;
}
}
No, there’s no way around that. Ref parameters are already a hack that uses actual pointers to the memory location of the variable. Due to this fact such pointers can not be stored in any variable since such pointers could even point to a variable on the stack of the calling method. So storing it for later use is completely impossible.
It’s a different story when you use a lambda expression. In this case the compiler will create a closure object that wraps the variable into an internal class. That class can now be referenced by any code that wants to access this variable. Though closures are just ordinary C# classes which use ordinary C# references and not memory pointers.
ps: You can simplify your lambda expression to:
StartCoroutine(Foobar(() => SomeBool) );
This of course is only possible if the lambda represents a single statement / expression. Using a method body explicitly allows more complex code, though it also requires the use of the return statement.
Thanks Bunny… I just like big blocks of vertical code with lotsa whitespace… maybe it’s because back in the Z80 / 6502 assembly days we did one thing per line and we LIKED it.
Either way, seems I always gotta stick a breakpoint or a Debug.Log in there so… “why not have it all aired out?” is my reasoning.
EDIT: vertical spacing also makes version control diff logs SO much more meaningful, revertable, cherry-pickable, etc.
CLC clear the carry
LDA $20 get the low byte of the first number
ADC $22 add to it the low byte of the second
STA $24 store in the low byte of the result
LDA $21 get the high byte of the first number
ADC $23 add to it the high byte of the second, plus carry
STA $25 store in high byte of the result
I just find scanning through stuff kinda hard but that’s a really good point, and you can fold things up easy enough.
Hey I was a little confused by the box code, but does that work similarly to how you can reference an object in an IEnumerator and get updated values (Such as transform values) from that?