-
-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using serialization for saving and loading #67
Comments
seems like a bug! thanks for reporting, i'll work on a fix ASAP |
@luetkemj it seems this error indicates that import {
Types,
createWorld,
defineSerializer,
defineDeserializer,
defineComponent,
registerComponent,
addEntity,
addComponent
} from "bitecs"
const save = (world) => {
const serialize = defineSerializer(world)
return serialize(world)
}
const load = (world, packet) => {
const deserialize = defineDeserializer(world)
deserialize(world, packet)
}
const worldA = createWorld()
const worldB = createWorld()
const C = defineComponent({
x: Types.f32,
y: Types.f32,
z: Types.f32,
})
registerComponent(worldA, C)
// uncomment this line and the error goes away
// registerComponent(worldB, C)
const eidA = addEntity(worldA)
addComponent(worldA, C, eidA)
const packet = save(worldA)
load(worldB, packet) because serializers and deserializers need to have the same exact config up-front, using worlds creates a bit of an edge case if those worlds don't know about the same exact components. to avoid this error one should explicitly pass in all components to both the serializer and deserializer for now. however, i will be introducing a new function to a future release which should make this easier: i'm now wondering if it's a bad idea to allow a const serialize = defineSerializer(getWorldComponents(world)) what do you think? |
@NateTheGreatt That feels a lot more intuitive to me. If there's a legitimate usecase for passing in the world directly that I'm missing, good documentation should make things clear about when to do what. |
Would also love to see some in-build solution for that! So far I've been using this to keep track of all components (to avoid forgetting to register one):
|
Thank you Nate for your code, I didn't realise you code register a component against a world, if I register all components then it works!!! also, in case anybody needs it, it took me a while how to actually save it to a file (in nodejs, I am using electron)
(I am unsure why it works but it works for me, which is good enough for now!) |
Serializing a world seems to work but deserializing throws the following error:
Here's some repro code:
I don't get an error if I use defineSerializer with same world that I serialized but I'm trying to figure out how that's useful. If I have the world already, there's no reason to deserialize.
Not sure if bug or I'm missing something...
The text was updated successfully, but these errors were encountered: