RiptideNetworking / Riptide

Lightweight C# networking solution for multiplayer games.
https://riptide.tomweiland.net
MIT License
1.06k stars 141 forks source link

[Bug] Some values not being read properly #132

Closed Charanor closed 4 months ago

Charanor commented 4 months ago

Hello! In my setup I am writing each entity to a message then combining the messages to pack them as tightly as possible before sending them over the net. I noticed when doing this some values are not written (or read, at least) correctly. The below snippet can reproduce the issue without having to send the messages over a connection.

Note that it doesn't matter if I use int/float/long or any other value, the second AddXYZ seems to always fail (or the second GetXYZ at least).

var m1 = Message.Create();
m1.AddInt(151415);
m1.AddInt(18165);
m1.AddBool(true);

var m2 = Message.Create(MessageSendMode.Notify);
m2.AddMessage(m1, m2.WrittenBits, 0);

var f1 = m2.GetInt(); // 151415 (correct)
var f2 = m2.GetInt(); // 0 (wrong!!)
var b = m2.GetBool(); // true (correct)

image

tom-weiland commented 4 months ago

In your example m2.AddMessage(m1, m2.WrittenBits, 0); should actually be m2.AddMessage(m1, m1.WrittenBits, 0);

Also, why not just write each entity directly into the combined message? Why the extra steps?

Charanor commented 4 months ago

I'm a dong xD sorry! And writing directly to the "main" message is the plan, I was just transferring the netcode from my previous iteration that used per-entity packages so this was sort of an intermediate step before I write the real serialization.