3mcd/javelin

Automatic ID generation

Closed this issue ยท 4 comments

Might be the second of a couple of issues I'll file as I begin digging into the library.

How do you feel about automatically assigning an incrementing ID to components? It can be a bit of a pain to have to manually manage something which would be pretty trivial for the computer to track.

Since components are already registered with the World, that seems like it would be a good place to automatically provide it a unique ID. That way you could conceivably even import third-party components from libraries, register them to the World, and their IDs would be guaranteed unique with your own components. Right now it would be difficult to pull in exotic components and be confident your manually selected IDs are not colliding with theirs.

3mcd commented

A primary goal of mine is to support client/server (maybe eventually P2P) networking. Ideally we can make it easy to share code between a frontend and game server. The reason that component ids/types are explicitly defined (right now) is to ensure the component type is uniquely identified on both ends of the "wire". Even if we added a seeded/pseudo-random id to components based on the order they are registered (or declared), it doesn't guarantee parity between client/server.

I totally agree that it's a drag and is a big limitation of the library. I've seen other libraries use strings instead of integers, which are easier to debug and provide fewer chances for conflicts, but inherently increase the overhead for each packet. I'd love to hear some suggestions as long as they address these two concerns.

Good reasoning. I'm not making a multiplayer game so it hadn't occurred to me.

For now it's simple enough to wrap the component factory that I've stopped worrying about this limitation.

If the intention is to remain multiplayer first in design, the current pattern makes sense. A more single player friendly api might auto-increment IDs, with the option to manually specify them if desired and document that this would be necessary in a networked game.

3mcd commented

Component types will be auto-registered and assigned ids automatically in v1.0. Assigning a specific type id will be done using registerComponentType.

import { component } from "@javelin/ecs"
const Position = {
  x: float64,
  y: float64,
}
component(Position) // `Position` type id automatically assigned

// OR
import { registerComponentType } from "@javelin/ecs"
registerComponentType(Position, 999)
3mcd commented

This is now available in the 1.0 alpha release(s). Component types are assigned an auto-incrementing id. You can specify an id using registerSchema:

import { registerSchema } from "@javelin/ecs"
const Position = { x: number, ... }
registerSchema(Position, 1)