This is a small drop-in code sample for binary LBP serialization.
The benefits of this method include:
- You need only one procedure for both seralization and deserialization. This way they can't go out of sync
- Full backwards compatibility with all previous versions
- no need for RTTI
Note
This doesn't serialize data automatically, you need to explicly write the procs. If you want more of a quick-and-dirty serialization I recommend you to check out marshalling in core:encoding/json
or core:encoding/cbor
The serializer versioning and generic serialize
procedure depend on other parts of your package, so the indented way of using this is to copy serializer.odin
into your game package.
Entity :: struct {
pos: [2]f32,
health: f32,
name: string,
foo: i32, // Added in version 'Add_Foo'
}
entity_serialize :: proc(s: ^Serializer, entity: ^Entity, loc := #caller_location) -> bool {
// useful for debugging. Set serializer.debug.enable_debug_print to true to enable logging
serializer_debug_scope(s, "entity")
serialize(s, &entity.pos) or_return
serialize(s, &entity.health) or_return
serialize(s, &entity.name) or_return
if s.version >= .Add_Foo do serialize(s, &entity.foo) or_return
return true
}
All contributions are welcome, I'll try to merge them when I have time!