Slick Forums

Discuss the Slick 2D Library
It is currently Thu Apr 18, 2019 2:17 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Sep 25, 2013 1:46 pm 
Offline

Joined: Sat Feb 23, 2013 8:04 pm
Posts: 10
Consider the following:
Code:
public class Main
{
   public static void main(String[] args)
   {
      World world = new World();
   
      world.setSystem(new SystemA());
      world.setSystem(new SystemB());
      
      Entity e = world.createEntity();
      e.addComponent(new ComponentX());
      e.addToWorld();
      
      world.process();
   }
}

public class SystemA extends EntityProcessingSystem
{
   public SystemA()
   {
      super(Aspect.getAspectForAll(ComponentX.class));
   }

   @Override
   protected void process(Entity e)
   {
      e.removeComponent(ComponentX.class);
      e.changedInWorld();
   }
}

public class SystemB extends EntityProcessingSystem
{
   @Mapper ComponentMapper<ComponentX> xm;

   public SystemB()
   {
      super(Aspect.getAspectForAll(ComponentX.class));
   }

   @Override
   protected void process(Entity e)
   {
      ComponentX x = xm.get(e);
      
      System.out.println(x.text);
   }
}

SystemA deletes the component, but the entity will still be in the list of SystemB since all updates are run at the beginning of World.process(). This will result in a NullPointerException when SystemB tries to access it.

An easy fix is to add a method to World:
Code:
public void checkChanged()
{
   check(changed, changedPerformer);
}

And call it after e.changedInWorld().

You should probably also add corresponding methods for added, deleted, enabled and disabled entities as well.

_________________
/Peter


Top
 Profile  
 
PostPosted: Sat Sep 28, 2013 8:10 pm 
Offline

Joined: Sun Jun 03, 2012 9:21 pm
Posts: 49
Thanks, I've always coded around said problem - thinking I'd investigate the cause some other time. I've updated the artemis fork I maintain - since the main artemis repo doesn't appear to be actively maintained anymore - I thought I'd mention it here.

_________________
agrotera: bytecode-weaving antiboilerplate lib for artemis. | artemis-odb: bugfixing and performance optimized fork.


Top
 Profile  
 
PostPosted: Sun Sep 29, 2013 9:42 am 
Offline

Joined: Sat Feb 23, 2013 8:04 pm
Posts: 10
I'm also wondering if the call to Entity:changedInWorld() couldn't be done automatically by the add/removeComponent() methods. The only thing that changedInWorld() does is to add a reference to the entity in World.changed. Unless you add/remove huge amounts of components to your entities every frame, I don't think it'll be much of an performance impact.

I'm also working on my own version of Artemis. And I think it would be nice if you could just add/remove components/entities and have it work without having to call extra methods or checking for null pointers.

_________________
/Peter


Top
 Profile  
 
PostPosted: Sun Sep 29, 2013 11:13 am 
Offline

Joined: Sun Jun 03, 2012 9:21 pm
Posts: 49
Hmm... I still think it's preferable to to not call changedInWorld() unnecessarily, though I agree it would be nice if we could omit the call from our code. What about adding some flag to EntitySystem - something like isEntityChange and/or Entity.hasChanged - and add a hook inside processEntities()?

SineNomine wrote:
'm also working on my own version of Artemis. And I think it would be nice if you could just add/remove components/entities and have it work without having to call extra methods or checking for null pointers.


Cool, is it available somewhere?

_________________
agrotera: bytecode-weaving antiboilerplate lib for artemis. | artemis-odb: bugfixing and performance optimized fork.


Top
 Profile  
 
PostPosted: Mon Sep 30, 2013 5:31 am 
Offline

Joined: Sat Feb 23, 2013 8:04 pm
Posts: 10
No it's not, I'm afraid. But it's basically your fork with just a couple of tiny changes. Eg. I added a name field to entities in order to quickly find out what kind of entity it is when debugging.

_________________
/Peter


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group