Example of event readers? #76
-
I found the EventBus and I like the idea of tracking entities added and removed systematically like this. But I'm struggling to figure out the right pattern to do this. Specifically I'm not sure where my own callbacks fit in. I can do the following:
But I don't think this is right; that callback never gets called when I subsequently do I've searched around the example code and couldn't find anything salient. Perhaps I'm not meant to interact with the event bus in this way? Thanks in advance for any pointers! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 25 replies
-
Did you see the events example? Events usually should be run from systems and use the standard reader and writer. In order to listen for added entities, you could write a system like this: const EventListenerSystem = createSystem({
entityAddedEvents: ReadEvents(SimECSAddEntityEvent),
}).withRunFunction(({entityAddedEvents}) => {
let event;
for (event of entityAddedEvents.iter()) {
console.log('Entity Added!', event.entity.id);
}
}).build(); If you really want to create your own reader, you'll want to also listen to the correct event name for added entities (SimECSAddEntityEvent). In your code, you also don't need to use line 2. You could even create your very own event bus outside of the ecs, by instantiating the classes yourself and managing them as you see fit, or implement new classes against the interface in order to switch out parts. This is unfortunately missing from the docs. I'll try to write up something about it. You can find the internal event definitions here |
Beta Was this translation helpful? Give feedback.
-
Trying this now and running into an odd structural issue? I admit I am clueless about modern JS module systems and my googling makes it seem like this is often a build side issue. But I'm not having much luck running it down on that side; everyone else with this issue seem to be doing weirder stuff. Perhaps it will ring a bell for you.
All my other
I wonder if this is a consequence of the internal events not being exported from here? Or do you think it's something to do with my own vite-specific build patterns or how I'm importing the classes? I last wrote javascript in any depth ~10 years ago so all these tools changed out from under me and I'm rebuilding my knowledge. |
Beta Was this translation helpful? Give feedback.
Did you see the events example? Events usually should be run from systems and use the standard reader and writer.
In order to listen for added entities, you could write a system like this:
If you really want to create your own reader, you'll want to also listen to the correct event name for added entities (SimECSAddEntityEvent). In your code, you also don't need to use line 2.
eventBus.createReader()
already regis…