3

I really want to know if there is a way to define action variables async? Or some alternative method?

public System.Action myAction; public async System.Action myAsyncAction; void Start() { // normal action myAction += () => { Debug.Log("Inject some code in runtime.."); }; // I want something like this that support wait time.. myAsyncAction += () => { await Task.Delay(2000f); Debug.Log("Inject some code in runtime.."); }; } 
2
  • 1
    But Action is for void, and awaitables return something like a Task if they have no result value, or Task<T> if they have a result value. In either case the Task is the return value, so they aren't void (aren't Actions). Don't strive for ways to await void ;) Commented May 8, 2022 at 7:27
  • The case that Clinton mentioned. Fixed the problem. In fact, I was looking for a way to add code to the body during runtime. :)) thank u guys. Commented May 8, 2022 at 15:50

1 Answer 1

6

I've used Func<Task> in the past. EG:

Func<Task> asyncMethod = async () => { await Task.Delay(1000); Console.WriteLine("done here"); }; await asyncMethod(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Think there might be a better suffix than "..Action"
Fair enough @CaiusJard, changed to asyncMethod
Between asyncAction and asyncMethod, I would prefer the former. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.