Hi there!
I’m trying to write Commnad/ClietnRpc code, but struggled in weird behavior.
Here is code:
[Command]
void CmdProcessMessage(StarMessage message)
{
if (!isServer)
return;
mp.ProcessMessage(ref message);
RpcReturnMessage(message);
}
[ClientRpc]
void RpcReturnMessage(StarMessage message)
{
if (!isClient)
return;
if (responseHandlers.TryGetValue(message.MessageId, out var msg))
{
msg(message);
}
}
public struct StarMessage
{
public long MessageId { get; set; }
public bool Returnable { get; set; }
public string Command { get; set; }
public byte[] Data { get; set; }
}
So, I calling server CmdProcessMessage(), and sending data, like messageId, and serialized data;
Server is receiving this data without issue, processed it, and sending results to client by calling RpcReturnMessage(), and sending message with serialized answer. Before RpcReturnMessage() call, on server, message is absolutly right - have correct messageId, data array, etc.
But, when RpcReturnMessage is called on client, the ‘message’ parameter have every field as default! - messageId=0, command = null, data=null. (like if I’d called new StarMessage())
HOW?
If referring to ClientRpc documentation ClientRpc
The allowed argument types are:
• Basic type (byte, int, float, string, UInt64, etc)
• Arrays of basic types
• Structs containing allowable types
So my struct is passing this rules.
Before this I tried to use class for this, and had the exacly same behaviour - everything great right to moment of RpcReturnMessage call - again, message in RpcReturnMessage have everything defaulted, so no values were passed by network, and I don’t know the reasons. And after reading of documentation, I was that his thing can pass only structs, so I changed type of my messages (with logic), and this issue is still reproducing.