- SendNamedMessage cannot receive messages from the host?
The message is received fine by non-host clients.
When trying to receive a message from the host
OverflowException: Attempted to read without first calling TryBeginRead() error occurs
Below is the code used.
public void SendMessageToClient(ulong clientId, string message, MessageName messageName = MessageName.Message)
{
using FastBufferWriter writer = new FastBufferWriter(8, Allocator.Temp, 256);
writer.WriteValueSafe(message);
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(messageName.ToString(), clientId, writer);
}
- I’m trying to implement a guild chat feature for my game. Which is better, Rpc or SendNamedMessage?
Yes that works, I just implemented this today. The host receives the message as a direct method call.
That’s an issue with serialization. Check how you are deserializing the data in the receive method. You may have used ReadValue rather than ReadValueSafe.
RPCs are built upon custom messages. RPCs are easy, flexible and straightforward to use. With custom messages you have to write more code.
But sometimes you need more control over the process, specifically when it comes to serialization or when it comes to sending/receiving messages on non-NetworkObject objects (which is my use-case - I don’t want to force the systems I work with into NGO’s NetworkObject pattern, I just need messaging on established classes).
Custom messages also allow for easier bandwidth optimization. In my test I had 100 NetworkObjects moving, each with a NetworkTransform. The host’s upstream traffic was 230 Kb/s even with all optimizations checked (eg half float, only send xyz and rotation y). Now I send all 100 objects’ positions + rotation y with a single writer, 8 bytes per object (4 half-floats). Without compression the traffic went down to 40-45 Kb/s simply because of batching, with compression it may be half of that or even less, and then I’m going to further decrease the send rate for these messages so I end up with 1 Kb/s per continuously moving object.
1 Like
Wow thank you so much your advice solved the problem!!!
1 Like