Comments (5)
Thanks Michael, it's great to hear that somebody's actually using this!
IIUC you have code that gets external objects as input (e.g., events from a framework) and would like to update corresponding entities in response. There's indeed no convenient way to do this right now. The __id
is internal, but even if it wasn't there's no way to fetch an entity by ID. And the Entity
instances themselves are flyweights and can get reassigned on the fly to different IDs.
My plan here is to allow you to "hold
" entity objects to ensure that they don't get reassigned, so you can safely refer to them either directly from external objects or through an intermediate map. This will carry a small overhead but should be fine as long as you're not holding new entities every frame. For example, holding an entity when you create the corresponding external object ought to be OK.
Would this work for you? It's not implemented yet but I could throw it together pretty quickly, I think.
In the meantime, any entities that are in the results of a query will also be "stable" so as a workaround you can establish such queries (even if you don't use the results) and safely refer to those entity objects. You'd have to be careful about timing, though, since if you add a new entity in a system the query won't capture it until the next frame, and any reference you hold to the entity in the meantime is subject to flyweight reassignment.
It is fairly simple to maintain a mapping from entities to external elements
BTW, are you using Type.object
for this, or some other approach?
from becsy.
Thanks heaps for the quick response!
IIUC you have code that gets external objects as the input (e.g., events from a framework) and would like to update corresponding entities in response.
Yep! My intention is to update entities in response to external events - and also to use the entities to update buffers on the GPU.
My plan here is to allow you to "hold" entity objects to ensure that they don't get reassigned, so you can safely refer to them either directly from external objects or through an intermediate map.
That sounds really powerful! Do you have any thoughts on how that'd play with write change tracking? We'd be holding onto entities for quite a while - rather than allocating per frame, so I think that'd be fine for our use case.
There's indeed no convenient way to do this right now. The __id is internal, but even if it wasn't there's no way to fetch an entity by ID.
Out of interest - I understand the Entity
object is recycled, but is the EntityID still stable for the life of an entity (i.e. until it's deleted)? Even without a way to fetch an entity given this id, that'd be a useful thing to have - as it'd make it easy to reconcile changes outside the ECS.
BTW, are you using Type.object for this, or some other approach?
We were putting the external identifier into a component and had an external map that we were performing lookups into.
from becsy.
Do you have any thoughts on how that'd play with write change tracking?
Holding entities wouldn't affect write change tracking, as that happens at the component level anyway so it doesn't matter which entity object the write goes through.
Out of interest - I understand the Entity object is recycled, but is the EntityID still stable for the life of an entity (i.e. until it's deleted)?
Yes, entity IDs are stable, but they're aggressively reused after an entity is deleted so I consider it unsafe to expose them as you could easily end up referencing something random (especially as there's no hooks for entity removals). In fact, dealing with deleted entities is the main challenge in implementing the proposed hold mechanism, but at least I have access to the internal queues and routines.
We were putting the external identifier into a component and had an external map that we were performing lookups into.
You might want to try Type.object
or Type.weakObject
then. It should feel more natural and probably even perform a bit better.
Anyway, I'll take a swing at the problem soon and let you know how it goes.
from becsy.
All right, Entity.hold
is implemented in 0.8.2. I also added safeguards against keeping a flyweight Entity
object around, though those reduced entity create/delete performance by 50% in dev. Make sure you're using becsy/perf
in production!
from becsy.
Amazing, thanks for looking into that, I'll give it a go.
from becsy.
Related Issues (20)
- Attach returns system box instead of system HOT 3
- Stricter types for `Query` and `QueryBuilder`? HOT 8
- System support for deferring work to be done on next execute HOT 3
- How to use dictionary and other reference type in the component? HOT 1
- Graph.findCycles() does not always find cycles HOT 2
- Terminating the world and creating a new one with the same components results in an error HOT 5
- can this be use in Phaser 3? HOT 3
- Ability to run a `dispose` method on systems in a system group HOT 3
- Command pattern for Systems HOT 7
- World.terminate() never resolves HOT 3
- ChainAlert: npm package release (0.13.1) has no matching tag in this repo
- Cannot read properties of undefined (reading 'trackedWrites') HOT 4
- Ability to refer to entities by identifiers stored in an external data structure HOT 6
- Internal error: Should commit log before counting. Please report a bug! HOT 7
- Is there an API to see if Becsy is currently running in accessRecentlyDeletedData mode? HOT 3
- Injecting dependencies into Systems HOT 2
- Example demo, teypscript version reports error, cannot find attribute `track` on `QueryBuilder` HOT 1
- why use sparse set rather than archetype? HOT 1
- Hello, are you still updating? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from becsy.