#Chat system
1 messages · Page 1 of 1 (latest)
my chat system has two channels, global chat and reply chat
displaying chat content from different channels is determined by a enum "chatmode"
private ChatMode chatmode;```
each channel has a list<string> that store previous chat content (chat log), shown below:
```cs
private List<string> GlobalLogs = new List<string>();
private List<string> ReplyLogs = new List<string>();
the chatlogUI core function is to receive broadcasted messages from other player and self, then display it on chatlog. To do this, all the received messages will be recorded into the logs, shown below:
public void ReceiveBroadCast_Global(string sender,string retrievedInfo, string userid)
{
GlobalLogs.Add($"{sender} : {retrievedInfo}");
if (chatmode == ChatMode.Global)
{
PopulateChatLogs(sender,userid);
}
}```
```cs
public void ReceiveBroadCast_Reply(string sender, string retrievedInfo, string userid)
{
this.username = sender;
this.userid = userid;
ReplyLogs.Add($"Reply from ({sender}) : {retrievedInfo}");
if (chatmode == ChatMode.Reply)
{
PopulateChatLogs(sender,userid);
}
}```
as u see on these receiver method, they will then call populateChatLogs() which will populate chatlog with the previous message recorded in the list i mentioned .
public async void PopulateChatLogs(string username,string userid)
{
switch (chatmode)
{
case ChatMode.Global:
//instantiate chat history
for (int i = 0; i < GlobalLogs.Count; i++)
{
AsyncOperationHandle<GameObject> handle = Addressables.InstantiateAsync("ChatElementUI", chatlogAnchor.transform);
await handle.Task;
GameObject go = handle.Result;
ChatElementUI ceui = go.GetComponent<ChatElementUI>();
ceui.initiate(GlobalLogs[i], username, userid);
}
break;
.....```
my question is , how to bind userid to list<string> logs itself
based on above setup, logs will only record the chat history, its completely separated from userid
i tried to merge userid and username into the log and then break it down by substring , my team lead stopped me from doing that tho
Use a tuple or a struct/class instead of a string for the list type . . .
ok i will look up tuple
@opaque shadow something like this ?
private List<Tuple<string, string, string>> globalLogs = new List<Tuple<string, string, string>>();
Tuple<string, string, string> log = new Tuple<string, string, string>(sender, retrievedInfo, userid);
globalLogs.Add(log);```
👍
self note : delete username/userid