Comments (8)
bug fixed without defineSystem
🙌
shoutout to @vegeta897 for the solution via discord chats 🙏
great catch @luetkemj!
0.3.21
published with the fix 👍
from bitecs.
Thanks @vegeta897 for helping explain the issue!
from bitecs.
i think what might be confusing you is the fact that bitECS recycles entity IDs.
This is not the problem; their test is not adding any new entities after the initial set.
Here is a simplified version of their test with only 3 entities:
- Create entities A B and C
- Add component to A and B
- Run system, query contains A and B and the system removes component from them
- Add component to B and C
- Run system, query contains only entity C, expected B and C
const world = createWorld();
const InFov = defineComponent();
// create entities A B and C
const entA = addEntity(world)
const entB = addEntity(world)
const entC = addEntity(world)
// add fov component to A and B
addComponent(world, InFov, entA);
addComponent(world, InFov, entB);
// if the inFovQuery is defined outside of the fovSystem
// it only returns new entities that match the query
const inFovQuery = defineQuery([InFov]);
export const fovSystem = (world) => {
// if it is defined inside of the query it returns all entities that match the query
// const inFovQuery = defineQuery([InFov]);
const inFovEnts = inFovQuery(world);
console.log(`expect: 2, actual: ${inFovEnts.length}`);
for (let i = 0; i < inFovEnts.length; i++) {
const eid = inFovEnts[i];
removeComponent(world, InFov, eid);
}
return world;
};
fovSystem(world);
// add fov component to B and C
addComponent(world, InFov, entB);
addComponent(world, InFov, entC);
fovSystem(world);
expect: 2, actual: 2
expect: 2, actual: 1
from bitecs.
oh right! i just noticed that. let me ponder this for a bit longer...
from bitecs.
okay, bug identified! not sure if i can fix this without adding back defineSystem
but i shall ponder this for a bit.
basically, because removals are deferred (to avoid removals borking loops), defineSystem
used to call commitRemovals
. queries call this function, too. those two aformentioned addComponent
calls were indeed adding the component, but deferred removals were removing the component from both entities the next time the query was called! 😆
thanks much for creating the issue and persisting through my short-sightedness! 😵
from bitecs.
i think what might be confusing you is the fact that bitECS recycles entity IDs.
const a = addEntity(world)
const b = addEntity(world)
const c = addEntity(world)
console.log(a) // => 0
console.log(b) // => 1
console.log(c) // => 2
removeEntity(world, a)
const d = addEntity(world)
console.log(d) // => 0
there shouldn't be any issues if you allow bitECS to handle all EID management:
import {
addComponent,
addEntity,
createWorld,
defineComponent,
defineQuery,
removeComponent,
} from "bitecs";
const world = createWorld();
const InFov = defineComponent();
// create 100 entities
for (let i = 0; i < 100; i++) {
const eid = addEntity(world)
// only add component to 25 entities
if (i < 25) addComponent(world, InFov, eid);
}
const inFovQuery = defineQuery([InFov]);
export const fovSystem = (world) => {
const inFovEnts = inFovQuery(world);
console.log(`expect: 25, actual: ${inFovEnts.length}`);
for (let i = 0; i < inFovEnts.length; i++) {
const eid = inFovEnts[i];
removeComponent(world, InFov, eid);
}
return world;
};
fovSystem(world);
// add fov component to 25 more entities
for (let i = 0; i < 25; i++) {
const eid = addEntity(world)
addComponent(world, InFov, eid);
}
fovSystem(world);
expect: 25, actual: 25
expect: 25, actual: 25
basically if you're calling addEntity
and not using the return value (which is essentially a random memory pointer) you're going to have "memory leaks" 😅
from bitecs.
these components are being added to EIDs which have been removed:
// add fov component to B and C
addComponent(world, InFov, entB); // entB was just removed from fovSystem()
addComponent(world, InFov, entC); // entC was just removed from fovSystem()
removed entities shouldn't appear in queries 😄 nor should components be able to be added to them!
from bitecs.
The system is removing components, not entities. Only your example is removing entities 😅
from bitecs.
Related Issues (20)
- Are you gonna port this library to TypeScript somehow? HOT 1
- What about boolean type in a component? HOT 5
- How to define two-dimensional array aka matrix? HOT 2
- Query operators HOT 2
- addComponent with initial value HOT 2
- null entity id HOT 1
- Serialize/Deserialize a single entity with components? HOT 1
- query for newly added Component
- Resizing smaller than the defaultSize always errors
- getEntityComponents does not return component name HOT 1
- How to push / pop from a list? HOT 1
- Query for mixed composition with Change HOT 1
- Could not find declaration file for module 'bitecs' HOT 1
- Is it necessary to make a query on every frame tick or are there any optimization tricks to avoid it if nothing changed? HOT 2
- defineComponent size not respected
- How to get MAX number for the current type? HOT 1
- Please publish a new version HOT 2
- Missing `readonly` in ComponentType
- Component sizes, multiple worlds HOT 1
- How to handle destroyed entities cleanup in central location HOT 4
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 bitecs.