- Home /
C# Invoke Function with Params and Delay
Is there a concise way to call the invoke function on a void with parameters, on a delay. I understand that you can call:
Invoke(string function, float delay)
However, that's not all too helpful when your function has parameter(s)
void writeToConsole(string text){
his is does.
Answer by ThomasVandenberghe · Mar 17, 2015 at 02:35 PM
You used to be able to if I remember correctly, but I can't find it in the docs. So my advice would be to use a Coroutine instead. It works kind of the same way.
StartCoroutine(Foo("Text", 2));
IEnumerator Foo(string text, float delay)
{
yield return new WaitForSeconds(delay);
// code here
}
Answer by JacobGames · Feb 17, 2016 at 05:00 PM
@ForgeStudios we have created a paid asset that can help you delay functions with parameters in just one line of code with no need to use coroutines.
It's called Super Invoke, it's like a more powerful Invoke.
Using Super Invoke your code would become:
float delay = 1f;
SuperInvoke.Run( ()=> writeToConsole("A delayed log"), delay);
You can also create sequences with methods and delays with which you can fully control a complex flow in a handy and easy-to-read way.
For instance you might want to write something to the console and after 1 second you want to instantiate an object. With Super Invoke it will be:
SuperInvoke.CreateSequence()
.AddMethod( ()=> writeToConsole("Hello World"))
.AddDelay(1f)
.AddMethod( ()=> Instantiate(object))
.Run();
That looks a bit like my (free) CoroutineHelper ;)
btw: your examples are missing a closing bracket in line 2 and 4 at the end
@Bunny83 thank you, missed those brackets.
CoroutineHelper looks great.
We're already working on further improvements of Super Invoke in which we'll add a repeat feature with ti$$anonymous$$gs and the choice of how many times the delayed call should be executed.
A useful additional feature would be a callback system.
Apart functionalities, perhaps one crucial component for a third party asset is the management of garbage. We are working on an internal pooling system which could potentially bring the run time allocation to 0 B.
In CoroutineHelper each method creates runtime allocations.
The first time Run.After is called it allocates 3.2 $$anonymous$$B due to the creation of the CoroutineHelper GameObject, it then allocates 464 B each frame probably due to the usage of the OnGUI method in the coroutine script (this allocation was not present before Run.After was called for the first time).
Subsequent calls of Run.After allocate 173 B.
SuperInvoke creates runtime allocations only when it is actually used, as for now it is between 50 B and 100 B, it optimizes some calls but the new pooling system will bring it close to 0 B.
We use Super Invoke for our own projects. The sequence feature helped us simplifying our work in many different ways. We thought it could be helpful to other people too.
Of course. It was never ment to be optimised for low garbage creation. Yes, the OnGUI thing could be optimised with a seperate script for those if needed.
The allocation for each Run.After call is expected and needed. Since the user can store and use the returned "Run" object I can't pool it and re-use it.
Also the creation of anonymous methods / lambda expression also creates garbage. It's even worse when you create a closure like in this case:
.Add$$anonymous$$ethod( ()=> Instantiate(object))
Answer by SimoGecko · Sep 27, 2020 at 04:25 PM
You can find how to do that here https://forum.unity.com/threads/tip-invoke-any-function-with-delay-also-with-parameters.978273/
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Update and Awake not being called. 1 Answer
Can you have multiple MonoBehaviors in the same script file? 1 Answer