ape-ecs icon indicating copy to clipboard operation
ape-ecs copied to clipboard

Feature Request: Support passing component class instead of string component names

Open jeremygiberson opened this issue 3 years ago • 2 comments

I'm not fond of hard coded string usage--especially in reference to code components because editors don't link the two. And code helpers like autocomplete, find references etc don't work w/ the string references.

I'm finding myself using the following to pass component references:

  const game = world.createEntity({
    id: 'game',
    components: [
      {type: Components.GameInfo.type()}
    ]
  });

Which I can make do with, but obviously I'd prefer if the library accepted either form (string or class) and got the type itself. Ie,

  const entity = world.createEntity({
    id: 'game',
    components: [
      {type: Components.GameInfo},
      {type: 'OtherComponent'}
    ]
  });

const gameInfo = entity.getOne(Components.GameInfo); // optionally support class reference
const otherComp = entity.getOne('OtherComponent'); // still support string name

jeremygiberson avatar Mar 11 '21 20:03 jeremygiberson

@jeremygiberson you can accomplish this using .name, which exists on classes. this could be moved into ape itself, so the queries will extract a .name if the filter isn't type string, but you should be able to do getOne(Components.GameInfo.name)

update: just tested and it works!

anderoonies avatar Jul 01 '21 00:07 anderoonies

second update: looks like this was already implemented~! https://github.com/fritzy/ape-ecs/blob/master/src/query.js#L90

anderoonies avatar Jul 01 '21 00:07 anderoonies