Zay-ES EntitySet.getAddedEntities, bug or expected?

Hi

I found this “issue” and i am not sure if its the expected behaviour or not.

Following scenario:

EntitySystem is running, and after a while i am creating a new EntitySet trough EntityData.getEntities(…).
What i would have expected is that all entities with the components specified are in the Set itself and in the addedEntities Set.

Turns out that the addedEntities Set is only get filled with entities meeting the requirements after the EntityData was created.

Not sure if i made clear myself. Its not a big issue since it is easy to workaround, just wanted to make sure if its expected or not.

Can’t understand this. The EntityData is surely created if the ES is running.

The entity sets’ sublists (changed, added and removed) are updated when you call the EntitySet.applyChanges() method. Typically, an entity is considered “added” if it was not in the set on last call, and is now.

Maybe you could paste some code?

When you get the entity set for the first time it may have entities in it already.

Typical scenario when getting an entity set:
EntitySet myEntities = ed.getEntities(…);
addMyObjects(myEntities);

…then go about any normal applyChanges() style updates later.

Note: I have created an EntityContainer class as part of my WIP Si02 utility set.

This makes it easy to manage entity to object pairings… or even just keep a set of entities in sync automatically.

But I’ll draw your attention to its start() method:

    public void start() {
        this.entities = ed.getEntities(filter, componentTypes);
        entities.applyChanges();
        addObjects(entities);   
    }

Edit: note I can provide a usage example if anyone is interested and has a use-case in mind. Else I can just provide one of my own.

I know that the set is already filled after creation, i just would have expected that the addedEntities set is also filled. Containing the same entities as the set itself.

All right after creation of course

Yeah, there was a reason I did it this way… but I don’t remember what it was. It must have been obvious at the time or I’d have written it down. Past me was wrong about that part, though. :slight_smile:

EntityContainer seems interesting. It’s a kind of entity observer. Do you plan to create a custom control to bind entities to JME spatials as an exemple?

I tend to use app states for that. So the app state would manage an EntityContainer instance that creates spatials. I have these all over the place.

Something like (off the top of my head pseudo-code-ish) :

public class ModelState extends BaseAppState {

    private EntityData ed;
    private Node modelRoot;
    private ModelContainer models;

    public ModelState( EntityData ed ) {
        this.ed = ed;
    }

    protected void initialize( Application app ) {
        modelRoot = new Node("myModels");
        models = new ModelContainer(ed);
    }
    
    protected void cleanup( Application app ) {
    }
    
    protected void onEnable() {
        models.start();
        ((SimpleApplication)app).getRootNode().attachChild(modelRoot);
    }
 
    public void update( float tpf ) {
        models.update();
    }
    
    protected void onDisable() {
        models.stop();
        modelRoot.removeFromParent();
    }
    
    private class ModelContainer extends EntityContainer<Spatial> {
        public ModelContainer( EntityData ed ) {
            super(ed, ...model-related components... position, modelinfo, etc...);
        }
        
        protected Spatial addObject( Entity e ) {
            Spatial result = create my model
            modelRoot.attachChild(result);
            
            // Reuse my own update for convenience
            updateObject(object, e);
        }
    
        protected void updateObject( Spatial object, Entity e ) {
            // update the spatial stuff for the entity
        }
    
        protected void removeObject( Spatial object, Entity e ) {
            object.removeFromParent();
        }    
    }
}

Something like that.

Accepted!

Yeah, creating models and attaching them to the scene is also the only usecase i have noticed this behaviour. I have a EntityObjectMapper with nearly the same functionallity as your Container.