Code Monkey home page Code Monkey logo

Comments (8)

NateTheGreatt avatar NateTheGreatt commented on May 18, 2024 4

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.

luetkemj avatar luetkemj commented on May 18, 2024 2

Thanks @vegeta897 for helping explain the issue!

from bitecs.

vegeta897 avatar vegeta897 commented on May 18, 2024 1

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:

  1. Create entities A B and C
  2. Add component to A and B
  3. Run system, query contains A and B and the system removes component from them
  4. Add component to B and C
  5. 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.

NateTheGreatt avatar NateTheGreatt commented on May 18, 2024 1

oh right! i just noticed that. let me ponder this for a bit longer...

from bitecs.

NateTheGreatt avatar NateTheGreatt commented on May 18, 2024 1

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.

NateTheGreatt avatar NateTheGreatt commented on May 18, 2024

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.

NateTheGreatt avatar NateTheGreatt commented on May 18, 2024

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.

vegeta897 avatar vegeta897 commented on May 18, 2024

The system is removing components, not entities. Only your example is removing entities 😅

from bitecs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.