From 43e02763e07c7c15d0365bf1a401dfe2f2ae8a8a Mon Sep 17 00:00:00 2001 From: JaimeGensler Date: Sat, 17 Jun 2023 11:52:58 -0700 Subject: [PATCH] Add getComponentId --- .changeset/tiny-trains-thank.md | 5 +++++ src/world/World.ts | 14 ++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 .changeset/tiny-trains-thank.md diff --git a/.changeset/tiny-trains-thank.md b/.changeset/tiny-trains-thank.md new file mode 100644 index 0000000..707547f --- /dev/null +++ b/.changeset/tiny-trains-thank.md @@ -0,0 +1,5 @@ +--- +'thyseus': minor +--- + +Add World.prototype.getComponentId diff --git a/src/world/World.ts b/src/world/World.ts index ed69932..0cf287e 100644 --- a/src/world/World.ts +++ b/src/world/World.ts @@ -180,6 +180,20 @@ export class World { this.entities.setRow(entityId, targetTable.length - 1); } + /** + * Gets the internal id for a component in this world. + * May differ from world to world. + * @param componentType The component type to get an id for. + * @returns The numeric id of the component. + */ + getComponentId(componentType: Struct): number { + DEV_ASSERT( + this.components.includes(componentType), + `Tried to get id of unregistered component "${componentType.name}"`, + ); + return this.components.indexOf(componentType); + } + #getTable(archetype: bigint): Table { let table = this.#archetypeToTable.get(archetype); if (table) {