Skip to content
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

Serialization for multiplayer #25

Open
krispya opened this issue Dec 28, 2024 · 0 comments
Open

Serialization for multiplayer #25

krispya opened this issue Dec 28, 2024 · 0 comments

Comments

@krispya
Copy link
Member

krispya commented Dec 28, 2024

Our implementation will be based on bitECS v0.4's https://github.com/NateTheGreatt/bitECS/tree/rc-0-4-0/src/serialization

// server
const serverWorld = createWorld()

// message components
const NetworkMessage = {}
const IdentityMessage = { entityId: [] }
// id msg setter
observe(serverWorld, onSet(IdentityMessage), (msgEid, value) => {IdentityMessage.entityId[msgEid] = value})

// msgs serialzier
const serializeNetworkMessages = createObserverSerializer(serverWorld, NetworkMessage, [IdentityMessage, ...otherMessageTypes])

// new player comes in
const newPlayer = addEntity(serverWorld)

// Add a new msg
const msg = addEntity(serverWorld)
addComponent(serverWorld, msg, NetworkMessage)
addComponent(serverWorld, msg, set(IdentityMessage, newPlayer))

const serializedData = serializeNetworkMessages();

// Remove the msg
removeEntity(serverWorld, msg);
// client
const clientWorld = createWorld()
const deserializeEvents = createObserverDeserializer(clientWorld, NetworkMessage, [IdentityMessage])

// Deserialize the events into clientWorld
deserializeEvents(clientWorld, serializedData)

// Process Identity Message
for (const eid of query(world, [NetworkMessage, IdentityMessage])) {
    const myEntity = IdentityMessage.entityId[eid]
    // process msg, then remove
    removeEntity(clientWorld, eid)
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant