❔ async await nooob trying to do the simplest of simple await for a string to be returned?

This is my Client code:
public static string GetName(NamingSystemType type, long id)
{
if (names.TryGetValue(type, out Dictionary<long, string> dict))
{
if (dict.TryGetValue(id, out string name)) return name;
else
{
CommsManager.Instance.CMD_RequestName(id);
return "";
}
}
else return "";
}
public static void AddName(NamingSystemType type, long id, string addingName)
{
if (!names.TryGetValue(type, out Dictionary<long, string> dict))
{
names.Add(type, dict = new Dictionary<long, string>());
}
if (dict.TryGetValue(id, out string name))
{
return;
}
else dict.Add(id, addingName);
}
public static string GetName(NamingSystemType type, long id)
{
if (names.TryGetValue(type, out Dictionary<long, string> dict))
{
if (dict.TryGetValue(id, out string name)) return name;
else
{
CommsManager.Instance.CMD_RequestName(id);
return "";
}
}
else return "";
}
public static void AddName(NamingSystemType type, long id, string addingName)
{
if (!names.TryGetValue(type, out Dictionary<long, string> dict))
{
names.Add(type, dict = new Dictionary<long, string>());
}
if (dict.TryGetValue(id, out string name))
{
return;
}
else dict.Add(id, addingName);
}
Await server to give name to client after request: This is client code aswell, which is called by ChatSystem for example:
internal static Task<string> GetNameAsync(long id)
{
// while(waitingForNameToBeAdded)??
How do I wait for server to addName as seen above?
}
internal static Task<string> GetNameAsync(long id)
{
// while(waitingForNameToBeAdded)??
How do I wait for server to addName as seen above?
}
36 Replies
Carly.TeamSail
Carly.TeamSailOP14mo ago
I guess for start should be GetNameAsync(long id)? So we actually know whuch ID to wait for? - EDIT: I fixed that part
Pobiega
Pobiega14mo ago
this can't be made async unless CommsManager.Instance.CMD_RequestName(id) is made async
Carly.TeamSail
Carly.TeamSailOP14mo ago
But CommsManager uses ServerRPC which runs code on Server only and cant return anything, so I think its impossible?
Pobiega
Pobiega14mo ago
¯\_(ツ)_/¯
Carly.TeamSail
Carly.TeamSailOP14mo ago
// Client calls this
public void CMD_RequestName(long id)
{
ServerRequestName(id);
}
// This runs on server only, and cant return anything because client doesnt run it/know about it
[ServerRpc(RequireOwnership = false)]
void ServerRequestName(long id, NetworkConnection conn = null)
{
NameToTarget(conn, id, ASO.Server.Server.CharacterSystem.CharactersByID[id].CharacterName);
}

// This runs on server and client
[TargetRpc]
void NameToTarget(NetworkConnection conn, long id, string name)
{
ASO.Client.ClientNamingSystem.AddName(NamingSystemType.CharacterName, id, name);
}
// Client calls this
public void CMD_RequestName(long id)
{
ServerRequestName(id);
}
// This runs on server only, and cant return anything because client doesnt run it/know about it
[ServerRpc(RequireOwnership = false)]
void ServerRequestName(long id, NetworkConnection conn = null)
{
NameToTarget(conn, id, ASO.Server.Server.CharacterSystem.CharactersByID[id].CharacterName);
}

// This runs on server and client
[TargetRpc]
void NameToTarget(NetworkConnection conn, long id, string name)
{
ASO.Client.ClientNamingSystem.AddName(NamingSystemType.CharacterName, id, name);
}
Pobiega
Pobiega14mo ago
I think you need to google very specifically about whatever RPC framework this is, and how to use async properly with it
Carly.TeamSail
Carly.TeamSailOP14mo ago
I know the namespace and server class should have different name and it looks like it was coded by shrek
Pobiega
Pobiega14mo ago
this is some unity stuff, I assume?
Carly.TeamSail
Carly.TeamSailOP14mo ago
Yes FishNetwork for Unity
Pobiega
Pobiega14mo ago
yeah, you'll need to check their docs
Carly.TeamSail
Carly.TeamSailOP14mo ago
ServerRPC cant return anything, and they never tested async ServerRPC but asked me to test it
Pobiega
Pobiega14mo ago
there is no silver bullet answer here
Carly.TeamSail
Carly.TeamSailOP14mo ago
But if ServerRPC cant return anything then async is impossible i think?
Pobiega
Pobiega14mo ago
¯\_(ツ)_/¯
Carly.TeamSail
Carly.TeamSailOP14mo ago
(╯°□°)╯︵ ┻━┻
Pobiega
Pobiega14mo ago
how does your code get the name, if the RPC call cant return anything?
Carly.TeamSail
Carly.TeamSailOP14mo ago
TargetRPC
Pobiega
Pobiega14mo ago
oh it RPCs back.. fuck thats so gnarly
Carly.TeamSail
Carly.TeamSailOP14mo ago
sends the name to specific connection, in this case the connection that requested it
Pobiega
Pobiega14mo ago
you'll need to build your own statemachine around that most likely
Carly.TeamSail
Carly.TeamSailOP14mo ago
oof fug I will just use callback lmao FishNetwork is 100% callbacks internally aswell so probably for a good reason I guess ill add another gnarly callback so it just works thanks for your time
public static string GetName(NamingSystemType type, long id, Action<string> callback)
{
if (names.TryGetValue(type, out Dictionary<long, string> dict))
{
if (dict.TryGetValue(id, out string name)) return name;
else
{
// callback when ready somehow?
CommsManager.Instance.CMD_RequestName(id);
return "";
}
}
else return "";
}
public static string GetName(NamingSystemType type, long id, Action<string> callback)
{
if (names.TryGetValue(type, out Dictionary<long, string> dict))
{
if (dict.TryGetValue(id, out string name)) return name;
else
{
// callback when ready somehow?
CommsManager.Instance.CMD_RequestName(id);
return "";
}
}
else return "";
}
Is this possible @Pobiega ? (sorry for ping)
Pobiega
Pobiega14mo ago
? no a method cant get its returnvalue from another method (thats called remotely lol)
Carly.TeamSail
Carly.TeamSailOP14mo ago
So I need to store ID, and when name is received, check for stored IDs that were waiting and then return the name somehow
Pobiega
Pobiega14mo ago
yes you could have your "GetName" method poll the place where names get put like, check for answer, wait 1ms, try again
Carly.TeamSail
Carly.TeamSailOP14mo ago
Hmm I can use async await for that? Task.Delay(10) or something?
Pobiega
Pobiega14mo ago
sure
Carly.TeamSail
Carly.TeamSailOP14mo ago
ok ok ill try thanks again!
async UniTaskVoid OnMessageReceived(long ID, byte channel, long senderID, string message)
{
string senderName = ClientNamingSystem.GetOrRequestName(NamingSystemType.CharacterName, senderID);

if (string.IsNullOrEmpty(senderName)) senderName = await WaitForName(senderID);
async UniTaskVoid OnMessageReceived(long ID, byte channel, long senderID, string message)
{
string senderName = ClientNamingSystem.GetOrRequestName(NamingSystemType.CharacterName, senderID);

if (string.IsNullOrEmpty(senderName)) senderName = await WaitForName(senderID);
async UniTask<string> WaitForName(long id)
{
string awaitedName;

// Wait 30 seconds max
for (int i = 0; i < 600; i++)
{
awaitedName = ClientNamingSystem.TryGetName(NamingSystemType.CharacterName, id);
if (string.IsNullOrEmpty(awaitedName)) await UniTask.Delay(50);
else return awaitedName;
}
return "";
}
async UniTask<string> WaitForName(long id)
{
string awaitedName;

// Wait 30 seconds max
for (int i = 0; i < 600; i++)
{
awaitedName = ClientNamingSystem.TryGetName(NamingSystemType.CharacterName, id);
if (string.IsNullOrEmpty(awaitedName)) await UniTask.Delay(50);
else return awaitedName;
}
return "";
}
This all happens inside the UI_Chat class, which is a Client-only class for displaying chat messages. EDIT: Changed to TryGetName - new method which doesnt request from server the name 600 times lol, it just checks if its arrived Or is this wetarded, i have no idea, i never used async await before tbh i mean i wrote it once without any errors in console but it turned out to be useless for that use case anyway and that was a few months ago
Pobiega
Pobiega14mo ago
uhm
Carly.TeamSail
Carly.TeamSailOP14mo ago
also UniTask == Task, its literally the same, only it works within Uity, which normal Tasks do not
Pobiega
Pobiega14mo ago
doesnt ClientNamingSystem.GetName(NamingSystemType.CharacterName, senderID) send the RPC request?
Carly.TeamSail
Carly.TeamSailOP14mo ago
yes
Pobiega
Pobiega14mo ago
you only want to run that once, not in the loop
Carly.TeamSail
Carly.TeamSailOP14mo ago
yes you are correct i will make a new method that only checks if its there and doesnt request again
Pobiega
Pobiega14mo ago
alr im gonna check out at this point, fever is spiking so I cant even think straight good luck
Carly.TeamSail
Carly.TeamSailOP14mo ago
Thanks and get well soon!
Accord
Accord14mo ago
Looks like nothing has happened here. 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