Interface, delegate, other? (C#, Unity)

Viewed 102

What I have:

public void New_Click()
{           
    MenuManager.GoToMenu(MenuName.NewDescr);
    StartCoroutine(WaitForClose());          
}

IEnumerator WaitForClose()
{
   yield return new WaitWhile(MenuManager.MenuOpen);
   listMonitor.AddToGrid(Descriptor.List.Last());
   yield return null;
}

What I want:

public void New_Click()
{
    MenuManager.GoToMenu(MenuName.NewDescr);
    WaitForClose(listMonitor.AddToGrid(Descriptor.List.Last()));
}

Basically, I'd like to implement a wrapper around a method call, but not have to specify StartCoroutine() nor write out the WaitForClose() method every time the method is used. I have little experience with delegates and Interfaces - are those the answer? If so, how?

2 Answers

You could generalize the WaitForClose() method you've shown by allowing an Action delegate to be passed. E.g.:

public void New_Click()
{           
    MenuManager.GoToMenu(MenuName.NewDescr);
    StartCoroutine(WaitForClose(() => listMonitor.AddToGrid(Descriptor.List.Last())));          
}

IEnumerator WaitForClose(Action action)
{
   yield return new WaitWhile(MenuManager.MenuOpen);
   action();
   yield return null;
}

Extending the above, you can then simplify the invocation a bit so it's closer to your "what I want" example:

public void New_Click()
{           
    MenuManager.GoToMenu(MenuName.NewDescr);
    WaitForClose(() => listMonitor.AddToGrid(Descriptor.List.Last()));          
}

void WaitForClose(Action action)
{
    StartCoroutine(WaitForCloseCoroutine(action));
}

IEnumerator WaitForCloseCoroutine(Action action)
{
   yield return new WaitWhile(MenuManager.MenuOpen);
   action();
   yield return null;
}

Peter had EXACTLY where I was going to go. But since you are completely new to the concept and will probably read more about it, I will start you with this.

The keyword "Action", instead of things like int, string, someClass, etc, you are telling this method that you want a procedure/method to be called when the specific point-in-time comes as Peter demonstrates by calling the lower-case "action()".

The other cool thing you can do is even allow actions that require some sort of additional parameters. Maybe you want to conditionally have some time delay to allow, values to compute, or whatever else. You can specify what data type parameters you want to pass in as well. The only difference is the signature such as

public void SomeFunction( Action<int> doSomething )
{
    // suppose some randomized value that you want the function to handle
    var someRand = SomeIntValueFromSomeRandomFunction();
    // notice the Action signature is saying it should be a function that 
    // will expect and integer data type to be passed to it when actually called.
    // above I get some random number, then call that Action method with it.
    doSomething( someRand );
}

Similarly you can do things with FUNCTIONS that RETURN values. The only difference is the last parameter is the expected data type being returned AFTER the function is called.

Using ACTIONS Using FUNCTIONS as well

Related