I have a WaitAndRun
function. I want this method to be very generic.
public static IEnumerator WaitAndRun<T>(float time, Action<T> action, object arg)
{
yield return new WaitForSeconds(time);
action((T)arg);
}
It works perfectly with the following example:
void an_action(int x)
{
Debug.Log(x);
}
//...
WaitAndRun<int>(1, an_action, 2);
The problem is I want to use the same definition when I need multiple args as well. How can I make this function support calling single and multiple args at the same time? For instance:
void another_action(int x, int y);
If I useobject[] args
instead of object arg
, I am not sure about how to modify the remaining part.
Edit: changed func to action
CodePudding user response:
Here's an alternative suggestion: Use lambda expressions and their ability to capture local variables:
public static IEnumerator WaitAndRun(float time, Action action)
{
yield return new WaitForSeconds(time);
action();
}
Usage:
WaitAndRun(1, () => an_action(2));
WaitAndRun(1, () => another_action(2, 3));
CodePudding user response:
Here is the standard way it is done in the framework:
public static IEnumerator WaitAndRun<T>(float time, Action<T> action, T arg)
{
yield return new WaitForSeconds(time);
action(arg);
}
public static IEnumerator WaitAndRun<T1, T2>(float time, Action<T1, T2> action, T1 arg1, T2 arg2)
{
yield return new WaitForSeconds(time);
action(arg1, arg2);
}
public static IEnumerator WaitAndRun<T1, T2, T3>(float time, Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4>(float time, Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5>(float time, Action<T1, T2, T3, T4, T5> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6>(float time, Action<T1, T2, T3, T4, T5, T6> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7>(float time, Action<T1, T2, T3, T4, T5, T6, T7> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16);
}
Note that Action<...>
is defined only to 16 parameters.
I also changed func
to action
as a function strictly returns a value and I wanted to be very clear.