Slick Forums

Discuss the Slick 2D Library
It is currently Thu Sep 19, 2019 8:15 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue May 14, 2013 2:17 am 
Offline

Joined: Sat Feb 09, 2013 1:56 am
Posts: 3
Hi,

I've encountered a problem in my game where I have a particular Component which very few entities have. In the game loop, for each entity, I manually querry whether the entity has that particular component using
Code:
Component c = e.getComponent(Component.class);
if (c != null) { ... }

(I know this isn't computationally optimal, but the call only happens about once every 10 seconds or so, and it's not within a system).

I didn't detect my problem early on because I didn't have very many Entities in general. However, once I had over 64 entities, there was a problem: e.getComponent(Component.class) calls bag.get(int index), and bag.get(int index) blindly returns that element, without first checking that the index is within the array bounds.

Because the components Bag (in ComponentManager.getComponent(Entity e, ComponentType type)) maps entityId to Component, entityId's >= 64 might cause an ArrayIndexOutOfBoundsException (Specifically if that particular component has not been assigned to any entity's with ID >= 64 already. If it has, the problem is just pushed upwards with the Bag.grow() method).

Here's a short program designed to duplicate the error:
Code:
import com.artemis.Component;
import com.artemis.Entity;
import com.artemis.World;

public class Bug {

   private World world;
   
   public Bug () {
      world = new World();
      
      for (int i = 0; i < 100; i++) {
         Entity e = world.createEntity();
         if (i == 0) e.addComponent(new A());
         e.addToWorld();
      }
      
      world.initialize();
      world.process();
      
      for (int i = 0; i < 100; i++) {
         Entity e = world.getEntity(i);
         A component = e.getComponent(A.class);
         if (component == null) System.out.println(i);
      }
   }
   
   public static void main(String[] args) {
      new Bug();
   }
   
   private class A extends Component {}
}

You can see it work fine up through entity #63, but at 64 there is an out of bounds exception.
I'll probably just make my own build changing bag.get(int index) to
Code:
public E get(int index) {
   if (index > data.length) return null;
   return data[index];
}

_________________
SpaceshipWarrior and Tactical RPG demo/tutorials at
http://javagamexyz.blogspot.com/
https://code.google.com/p/javagamexyz/


Top
 Profile  
 
PostPosted: Tue May 14, 2013 2:27 am 
Offline

Joined: Sat Feb 09, 2013 1:56 am
Posts: 3
Whoops - I just noticed a similar post from December http://slick.javaunlimited.net/viewtopic.php?f=28&t=6027

Reading through them I'm not sure if it was totally resolved and if this is a separate issue or not. I'll be happy to delete this post and move it over there if you'd like.

_________________
SpaceshipWarrior and Tactical RPG demo/tutorials at
http://javagamexyz.blogspot.com/
https://code.google.com/p/javagamexyz/


Top
 Profile  
 
PostPosted: Tue Mar 18, 2014 9:42 am 
Offline

Joined: Sat Feb 23, 2013 8:04 pm
Posts: 10
I know this is an old thread, but I recently ran into the same problem as Suttle.

ComponentManager.getComponent() doesn't actually check if the entity possesses the asked for component before trying to get it, but there's an easy fix for it. This is what I did:

In ComponentManager.getComponent(), replace the following line:
Code:
if(components != null) {

with
Code:
if(components != null && components.isIndexWithinBounds(e.getId())) {

_________________
/Peter


Top
 Profile  
 
PostPosted: Wed Mar 19, 2014 5:16 pm 
Offline

Joined: Sun Jun 03, 2012 9:21 pm
Posts: 49
What the... I thought that was fixed. I've pushed the fix to my fork, I used your fix, SineNomines - thanks :)

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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


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:  
cron
Powered by phpBB® Forum Software © phpBB Group