Hello
I have already searched and tested a little, but since I do not know what I’m actually looking for, it was not crowned with success. I have several functions that should be dynamical called again after a delay, I’m just not really satisfied with the solution and think there is certainly a cleverer way to do it. Can someone give me a nudge in the right direction?
greetings
public static class Methods
{
public static IEnumerator StartCooldown(int Time, string FunctionToCall)
{
yield return new WaitForSeconds(Time);
switch (FunctionToCall)
{
case "functionA":
yield return Controller.instance.functionToCall = "functionA";
yield return Controller.instance.runFunction = true;
break;
case "functionB"
yield return Controller.instance.functionToCall = "functionB";
yield return Controller.instance.runFunction = true;
break;
}
}
public class Controller : MonoBehaviour
{
public string functionToCall;
public bool runFunction;
private void Update()
{
if (runFunction)
{
switch (functionToCall)
{
case "functionA":
ClassA.instance.functionA(var);
runFunction = false;
break;
case "functionB":
ClassB.instance.functionB(var);
runFunction = false;
break;
}
}
}
}
I’d recommend using Actions instead. You can think of an Action as a type of variable the stores a function, so you can call it later.
using System; // the Action class lives in System, so don't forget to add a using statement!
public static class Methods
{
public static IEnumerator StartCooldown(int Time, Action FunctionToCall) // takes an Action now
{
yield return new WaitForSeconds(Time);
FunctionToCall?.Invoke(); // This actually calls the function passed in
// The ?. syntax above checks if FuncTionToCall is null before trying to call it.
// If it is null, then this code will quietly ignore it and exit. This is a nice way of doing simple null checks
// Also note that I've completely deleted the "controller" class. You don't need it at all with this approach.
// That being said, if you really need some other script to call the function, you can create an Action variable
// and store it there.
}
}
// Usage -----------------
StartCoroutine(Methods.StartCooldown(0.5f, SomeFunctionName));
// Note, do not call SomeFunctionName (by using parenthesis - SomeFunctionName())
// Instead, just pass its name in.
// This will work for any function that returns void and takes no parameters
// You can also use a lambda expression, which might look scary if you've never seen it before
// but is super useful in some situations
StartCoroutine(Methods.StartCooldown(0.75f, () => { Debug.Log("Hello, lambda!"); } );
1 Like
Thank you both for your input
through your detailed explanation i now understand the purpose of actions
with the first function it will work fine with your two solutions, with the second function it is not planned to deactivate or delete the gameObject and the function needs several frames up to several seconds until it reaches the end to start the delay to then start again, Im very Sorry the example havent show this detail, my mistake, and would not pause the function self
on the second view i have the solution i dont call the logic staff in Update i call it out from the coroutine with a WaitForEndOfFrame statement at the end of the loop