kevinresol / exp-ecs

Experimental macro-powered Entity-Component-System game engine
59 stars 5 forks source link

can't remove system or entity in update() function . #8

Closed sonygod closed 3 years ago

sonygod commented 3 years ago
import exp.ecs.system.System;
import exp.ecs.node.Node;
import exp.ecs.Engine;

class PlayerC<Event> extends System<Event> {
    @:nodes var nodes:Node<Player, Weapon>;

    var num:Float = 1.;

    public function new() {
        super();
    }

    override public function onAdded(e:Engine<Event>) {
        super.onAdded(e);

        for (n in nodes) {
            n.player.idle();
            n.weapon.reset();

            n.weapon.shoot();
        }
    }

    override function update(dt:Float) {
        super.update(dt);

        num += num + num * dt;

        if (num > 10) {
            trace("desotry");

            for (node in nodes) {
                engine.entities.remove(node.entity);//not work.
            }

            engine.systems.remove(this);//not work.
        } else {
            trace(num);
        }
    }
}
kevinresol commented 3 years ago

what do you mean by "not work"?

sonygod commented 3 years ago

I mean when removing system components , it should be not call update() function again.

but currently it's still execute update function?

how to stop the update function?

kevinresol commented 3 years ago

entities and systems will be removed after the current update

sonygod commented 3 years ago
import exp.ecs.system.System;
import exp.ecs.node.Node;
import exp.ecs.Engine;

class PlayerC<Event> extends System<Event> {
    @:nodes var nodes:Node<Player, Weapon>;

    var num:Float = 1.;

    public function new() {
        super();
    }

    override public function onAdded(e:Engine<Event>) {
        super.onAdded(e);
    }

    override function update(dt:Float) {
        super.update(dt);

        trace("update");//still excute after engine.systems.remove(this, true);
        engine.systems.remove(this, true);
    }
}

hello ,still excuting after call engine.systems.remove(this, true);

I found the problem here

system.onAdded(engine);//why readd to engine system again after remove ?

override function operate(item:Item<System<Event>>, operation:Operation) {
        var system = item.data;
        switch operation {
            case Add:
                queue.add(item);
                system.onAdded(engine);
            case Remove | RemoveAndDestroy:
                queue.add(item);
                system.onAdded(engine);//why  readd  to engine system again after remove ?
                if(operation == RemoveAndDestroy)
                    system.destroy();
        }
    }
kevinresol commented 3 years ago

oh right that is obviously wrong

sonygod commented 3 years ago

@kevinresol will you take a look and fixed?

kevinresol commented 3 years ago

Should be simple and obvious to fix, how about a PR?

kevinresol commented 3 years ago

Please PR or it is a wontfix, since a new version is in the works