oxygen icon indicating copy to clipboard operation
oxygen copied to clipboard

Components cant be removed but can be marked for removal?

Open billHaggerty opened this issue 3 years ago • 3 comments
trafficstars

Current bug behaviour

If I add a component and then remove it, it does not get removed.

Expected behaviour

A component marked for removal would be removed.

Steps to reproduce


import 'package:oxygen/oxygen.dart';
import 'package:test/test.dart';

class TestComponent extends ValueComponent<void> {}

class ComponentAdderSystem extends System {
  late Query query;
  @override
  void init() {
    query = createQuery([HasNot<TestComponent>()]);
  }

  @override
  void execute(double delta) {
    for (final entity in query.entities) {
      entity.add<TestComponent, void>();
    }
  }
}

class ComponentRemoverSystem extends System {
  late Query query;
  @override
  void init() {
    query = createQuery([Has<TestComponent>()]);
  }

  @override
  void execute(double delta) {
    for (final entity in query.entities) {
      entity.remove<TestComponent>();
    }
  }
}

void main() {
  group('Component', () {
    test('Components should be added and removed from entities.', () {
      final world = World();
      world.registerComponent<TestComponent, void>(() => TestComponent());
      world.registerSystem(ComponentRemoverSystem());
      world.registerSystem(ComponentAdderSystem());
      var testEntity = world.createEntity('Test Entity');
      world.init();

      expect(false, testEntity.has<TestComponent>(),
          reason: 'Entity has component it shouldnt.');
      world.execute(1);
      testEntity = world.entities.first;
      expect(true, testEntity.has<TestComponent>(),
          reason: 'Entity does not have required component.');
      world.execute(1);
      testEntity = world.entities.first;
      expect(false, testEntity.has<TestComponent>(),
          reason: 'Entity has component it shouldnt.');
    });
  });
}

More environment information

  • Oxygen version: 0.2.0

More information

I created a fork where I made the changes that I thought would be needed here:

https://github.com/billHaggerty/oxygen/blob/main/lib/src/world.dart#L66 https://github.com/billHaggerty/oxygen/blob/main/lib/src/entity/entity_manager.dart#L157 https://github.com/billHaggerty/oxygen/blob/main/lib/src/entity/entity_manager.dart#L165 https://github.com/billHaggerty/oxygen/blob/main/lib/src/entity/entity_manager.dart#L112

This all seems like it was supposed to work. The components to be removed were being tracked but I couldn't see what was supposed to actually remove them. If I was missing something please let me know.

billHaggerty avatar Dec 09 '21 18:12 billHaggerty

I'm having issues with this bug as well. Any updates on if it will get fixed, or if I am using add/remove components wrong?

larakinsystems avatar May 08 '22 17:05 larakinsystems

I just ran into this issue as well. Tried removing a component and my system kept updating that entity. ~~Looks like for now I'll have to clone flame_oxygen and modify it for now to use your oxygen fork~~

I just needed to add your fork as a dependency of my project, forcing that as the only version.

natebot13 avatar May 14 '22 12:05 natebot13

Looks like in order to get this to actually work with flame_oxygen I had to add the new component removal function to FlameWorld's update function, since it doesn't use World's execute for some reason...

natebot13 avatar May 14 '22 13:05 natebot13