I am trying to create a tutorial where after user completes first task, hes given second task etc etc.
I am using Co-Routines. I dont want to create a separate co-routine for every step of the tutorial but instead want to pass the parameters to a generic co-routine function. I tried the following but the condition keeps evaluating as true even though city index has changed. Perhaps what I am attempting is not possible and for AI / Tutorials using co-routines one must write separate co-routine functions.
void Start ()
{
StartCoroutine(MyCoroutine(GameControl.control.cityindex != 5, "Click build CO icon to create a Central Office in St. Louis"));
StartCoroutine(MyCoroutine(GameControl.control.cities[5].centralOffice == null, "Select switchboard from equipment panel and place it on floor"));
}
IEnumerator MyCoroutine(bool condition, string mystring)
{
while (condition)
{
yield return new WaitForSeconds(0.5f);
}
displayManager.DisplayMessage(mystring);
}
a bool isn’t passed as a reference, it doesn’t get re-evaluated every time.
Instead you could pass in a delegate that does re-evaluate. Something like:
void Start()
{
StartCoroutine(MyCoroutine(() => { return GameControl.control.cityindex != 5; }, "Click build CO icon to create a Central Office in St. Louis"));
}
IEnumerator MyCoroutine(System.Func<bool> condition, string mystring)
{
while (condition())
{
yield return new WaitForSeconds(0.5f);
}
displayManager.DisplayMessage(mystring);
}
I think you mean it’s passed as a value - in that it’s passed as its value at the time it’s passed. The upshot being that future changes to that value won’t trickle down to methods it’s been passed to.
That being said - nothing about the OP code looks like a finite state machine. I can also frankly say, as someone who has done a coroutine based FSM implementation and subsequently ripped it out - you don’t want to do a coroutine based FSM implementation.
1 Like
I meant to say ‘isn’t’
my typing sucks sometimes…
this doesnt work. I get an error on the second squirlly bracket }
it says ; expected. but im not sure where to put it
check again, I added the appropriate ‘;’
sorry, really bad typing today.
Using a new computer, in linux mint (which I have yet to use), and everything has been sideways for me.
Yes this works now! thanks very much.
This is some pretty advanced C#. I checked the Unity Tutorials and scripts but it doesn’t mention this. Do you have any references to documentation to this So I can study it more and understand whats going on.
Well, it’s using delegates:
Specifically the System.Func delegate:
And when I pass the delegate in, I’m using an anonymous function: