Good afternoon, I am writing this post because I have some questions.
I have been doing two scripts one to get the money saved in the Wallet and I get {“money”: 15000} I would like to get only the value of “money” and I don’t know how to do it. Could you guide me a bit?
I used the following lines of code:
var account = await ServerConnect.Client.GetAccountAsync (ServerConnect.Session);
var Text = account.Wallet
The second is a chat. I already managed to send messages and load old messages, but the same thing happens to me again, I just want to get the string of the message content and it returns {“message”: “message’s content”}.
The chat script is this:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using Nakama;
using Nakama.TinyJson;
using TMPro;
public class ChatManager : MonoBehaviour
{
public ServerConnect ServerConnect;
[Header("Chat")]
public string ChatName;
public TMP_InputField ChatMessage;
[Header("Adding message")]
public GameObject MessageBox;
public GameObject MessagePrefab;
private IChannel ChatSession;
public async void StartChat()
{
var persistence = true;
var hidden = false;
ChatSession = await ServerConnect.Socket.JoinChatAsync(ChatName, ChannelType.Room, persistence, hidden);
Debug.LogFormat("Joined to chat channel: {0}", ChatSession);
ServerConnect.Socket.ReceivedChannelMessage += message =>
{
Debug.LogFormat("Received: {0}", message);
Debug.LogFormat("Message has channel id: {0}", message.ChannelId);
Debug.LogFormat("Message content: {0}", message.Content);
// Save message name
string messageString = "Message(" + message.Code + ")";
Debug.Log(messageString);
// Use InstMessage()
};
RestoreChat();
}
public async void SendMessage() {
var message = ChatMessage.text;
var content = new Dictionary<string, string> {
{"message", message}
}.ToJson();
var Sent = await ServerConnect.Socket.WriteChatMessageAsync(ChatSession, content);
Debug.Log(Sent);
ResetMessageBox();
}
public void ResetMessageBox() {
ChatMessage.text = "";
}
public async void RestoreChat() {
var result = await ServerConnect.Client.ListChannelMessagesAsync(ServerConnect.Session, ChatSession, 10, false);
foreach (var message in result.Messages)
{
Debug.LogFormat("Message id '{0}' content '{1}'", message.MessageId, message.Content);
string messageString = "Message(" + message.MessageId + ")";
InstMessage(messageString, message.Username, message.Content);
}
}
private void InstMessage(string messageString, string messageUsername, string messageContent) {
// Create object(id) with the message
var newMessage = GameObject.Instantiate(MessagePrefab, MessageBox.transform);
newMessage.name = messageString;
// Find name and message in the object <Find>
var Info = newMessage.transform.Find("Info").gameObject;
var NameSender = Info.transform.Find("Nickname").gameObject.GetComponent<TextMeshProUGUI>();
var Message = Info.transform.Find("Message").gameObject.GetComponent<TextMeshProUGUI>();
Debug.Log(Message);
// Set name and message in the object
NameSender.text = messageUsername;
Message.text = messageContent;
}
}
Now sorry for my ignorance if I am getting something very basic wrong. I want to use the InstMessage method to send new messages but by including it in:
ServerConnect.Socket.ReceivedChannelMessage
in StartChat ()
It gives me a constructor error.
I attach an image of how it looks when running the program.
As a last question I have for functions like ListChannelMessagesAsync()
Is there in the Nakama documentation what variables I can include or should I see them in the nakama asset code?
I appreciate if you can guide me in any of the points that I described in this topic.