I am having some trouble converting my games network communication from sending json to using flat buffers.
How my games client / server communication works:
We have a base class called GameMessage
[Serializable]
public class GameMessage
{
public abstract string TypeIdentifier { get; }
}
Then we have classes that inherit from GameMessage
for each type of data manipulation
[Serializable]
public class CreateEntity : GameMessage
{
public string typeIdentifier = "CreateEntity";
public override string TypeIdentifier => typeIdentifier;
public string EntityID;
}
[Serializable]
public class DeleteEntity : GameMessage
{
public string typeIdentifier = "DeleteEntity";
public override string TypeIdentifier => typeIdentifier;
public string EntityID;
}
[Serializable]
public class ChangeAttribute: GameMessage
{
public string typeIdentifier = "ChangeAttribute";
public override string TypeIdentifier => typeIdentifier;
public string EntityID;
public AttributeData Attribute;
}
In the json deserialization code, we read from the TypeIdentifier
parameter to identify the desired deserialization type. This allows the client to determine what type a message should be deserialized as, so in a nice way we can run handling behaviour based on the message type. This does not really work with flat buffers.
My question:
Is there a good way using flat buffers to generally define a table
(or type
as its referred to in C#) so the client can handle the messages without any explicit type checking.
FlatBuffer's union
feature is ideal to encode this kind of structure, and can do so without having to store or check identifier strings.