❔ UniTask / Unity - await/async connoisseurs wanted. Need help understanding
Is this correct?
I want to wait for Server (via CommsManager) to reply with
PlayerSelectionData[]
, after receiving that, I want to call UI_PlayerSelection.Instance.Init(psd);
BTW, UniTask is pretty much the same as C# Tasks, just a different keyword for example async void == async UniTaskVoid
.
CommsManager (method just for testing):
This seems to work... But I heard its incorrect, see replies below.
Thanks33 Replies
I was using events/callbacks before, but it was recommended to switch to async/await.
Just to test/learn async/await I changed the
CMD_TryRegisterAsync()
Method to this:
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Yes
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
UniTask is real async/await
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
It works exactly the same as async/await
TeBeCo
I suggest you add that to the title toward the left to make it visible
Quoted by
<@689473681302224947> from #await/async connoisseurs wanted. Need help understanding (click here)
React with ❌ to remove this embed.
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
I see
Somehow it does wait 1 sec and then init the selection panel correctly with the PSD's I created inside CommsManager
Can you please correct this if its incorrect?
Im an async/await noob
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
the scheduler is not the same the runtime is not the same the threadpool is not the sameI have very limited understanding of the above, except for understanding the broad concepts, like threadpool is the available threads, but no idea how that works, however I did read that async/await doesnt actually use additional threads at all, its some kind of C# black magic...
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Am I a thread in this ex?
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
What am i
oh
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
I see
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
The brains of the operation
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
the bitshifter of the operation
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
I understand the concept
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Just not how to write the code
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
I see, I use FishNet (high level networking) so its basically comparable, just call a method and the rest is done internally...
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
TLDR on
async
/await
:
* every .net API that is suffixed with Async
(eg: .Read()
and .ReadAsync()
) => use the Async
version
* if the API name ends with Async
=> await
it
* if the API returns a Task
=> await
it
* if you have to await
in a method:
* that method must have the async
keyword (you could even suffix your function name with Async
, up to you)
* if it was returning T
(eg:string
) => it should now return Task<T>
(eg: Task<string>
)
* APIs to ban and associated fix:
Thanks for your help and time, I updated the OP with my new code, hopefully someone with UniTask knowledge will jump in and correct it /improve it...
UniTask is just a wrapper on top of async/await + CustomYieldInstruction.
Yeah it's basically just async/await that respects Unity's frame timing
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.