❔ UniTask / Unity - await/async connoisseurs wanted. Need help understanding

public void TryRegister()
{
TryRegisterAsync().Forget(); // UI Register Button - Fire & Forget Method,
}
public async UniTaskVoid TryRegisterAsync()
{
PlayerSelectionData[] psd = await tryRegisterAsync();

UI_PlayerSelection.Instance.Init(psd);
gameObject.SetActive(false);
}
private async UniTask<PlayerSelectionData[]> tryRegisterAsync()
{
var email = _emailInput.text;
var pass = _passwordInput.text;

PlayerSelectionData[] psd = await CommsManager.Instance.CMD_TryRegisterAsync(email, pass);

return psd;
}
public void TryRegister()
{
TryRegisterAsync().Forget(); // UI Register Button - Fire & Forget Method,
}
public async UniTaskVoid TryRegisterAsync()
{
PlayerSelectionData[] psd = await tryRegisterAsync();

UI_PlayerSelection.Instance.Init(psd);
gameObject.SetActive(false);
}
private async UniTask<PlayerSelectionData[]> tryRegisterAsync()
{
var email = _emailInput.text;
var pass = _passwordInput.text;

PlayerSelectionData[] psd = await CommsManager.Instance.CMD_TryRegisterAsync(email, pass);

return psd;
}
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):
public async UniTask<PlayerSelectionData[]> CMD_TryRegisterAsync(string email, string pass)
{
await UniTask.Delay(1000);

var psd1 = new PlayerSelectionData("Player_1", 3);
var psd2 = new PlayerSelectionData("Player_2", 10);
return new PlayerSelectionData[] { psd1, psd2 };
}
public async UniTask<PlayerSelectionData[]> CMD_TryRegisterAsync(string email, string pass)
{
await UniTask.Delay(1000);

var psd1 = new PlayerSelectionData("Player_1", 3);
var psd2 = new PlayerSelectionData("Player_2", 10);
return new PlayerSelectionData[] { psd1, psd2 };
}
This seems to work... But I heard its incorrect, see replies below. Thanks
33 Replies
Carly.TeamSail
Carly.TeamSailOP14mo ago
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:
public async UniTask<PlayerSelectionData[]> CMD_TryRegisterAsync(string email, string pass)
{
await UniTask.Delay(1000);

var psd1 = new PlayerSelectionData("Player_1", 3);
var psd2 = new PlayerSelectionData("Player_2", 10);
return new PlayerSelectionData[] { psd1, psd2 };
}
public async UniTask<PlayerSelectionData[]> CMD_TryRegisterAsync(string email, string pass)
{
await UniTask.Delay(1000);

var psd1 = new PlayerSelectionData("Player_1", 3);
var psd2 = new PlayerSelectionData("Player_2", 10);
return new PlayerSelectionData[] { psd1, psd2 };
}
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
Yes
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
UniTask is real async/await
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
It works exactly the same as async/await
MODiX
MODiX14mo ago
TeBeCo
I suggest you add that to the title toward the left to make it visible
React with ❌ to remove this embed.
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
I see Somehow it does wait 1 sec and then init the selection panel correctly with the PSD's I created inside CommsManager
public void TryRegister()
{
TryRegisterAsync().Forget(); // UI Register Button - Fire & Forget Method,
}
public async UniTaskVoid TryRegisterAsync()
{
PlayerSelectionData[] psd = await tryRegisterAsync();

UI_PlayerSelection.Instance.Init(psd);
gameObject.SetActive(false);
}
private async UniTask<PlayerSelectionData[]> tryRegisterAsync()
{
var email = _emailInput.text;
var pass = _passwordInput.text;

PlayerSelectionData[] psd = await CommsManager.Instance.CMD_TryRegisterAsync(email, pass);

return psd;
}
public void TryRegister()
{
TryRegisterAsync().Forget(); // UI Register Button - Fire & Forget Method,
}
public async UniTaskVoid TryRegisterAsync()
{
PlayerSelectionData[] psd = await tryRegisterAsync();

UI_PlayerSelection.Instance.Init(psd);
gameObject.SetActive(false);
}
private async UniTask<PlayerSelectionData[]> tryRegisterAsync()
{
var email = _emailInput.text;
var pass = _passwordInput.text;

PlayerSelectionData[] psd = await CommsManager.Instance.CMD_TryRegisterAsync(email, pass);

return psd;
}
Can you please correct this if its incorrect? Im an async/await noob
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
the scheduler is not the same the runtime is not the same the threadpool is not the same
I 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
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
Am I a thread in this ex?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
What am i oh
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
I see
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
The brains of the operation
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
the bitshifter of the operation
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
I understand the concept
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
Just not how to write the code
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Carly.TeamSail
Carly.TeamSailOP14mo ago
I see, I use FishNet (high level networking) so its basically comparable, just call a method and the rest is done internally...
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX14mo ago
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:
var r = t.Result; /* ===> */ var r = await t;
t.Wait(); /* ===> */ await t;
var r = t.GetAwaiter().GetResult(); /* ===> */ var r = await t;
Task.WaitAll(t1, t2); /* ===> */ await Task.WhenAll(t1, t2);
var r = t.Result; /* ===> */ var r = await t;
t.Wait(); /* ===> */ await t;
var r = t.GetAwaiter().GetResult(); /* ===> */ var r = await t;
Task.WaitAll(t1, t2); /* ===> */ await Task.WhenAll(t1, t2);
Carly.TeamSail
Carly.TeamSailOP14mo ago
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...
Cattywampus
Cattywampus14mo ago
UniTask is just a wrapper on top of async/await + CustomYieldInstruction. Yeah it's basically just async/await that respects Unity's frame timing
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server