Ran into a crash today:
Code:
Caused by: java.lang.ArrayIndexOutOfBoundsException: 16
at com.artemis.utils.Bag.get(Bag.java:140)
at com.artemis.EntityManager.getComponent(EntityManager.java:117)
at com.artemis.Entity.getComponent(Entity.java:129)
at com.artemis.Entity.getComponent(Entity.java:140)
at com.gemserk.commons.artemis.components.Components.getTextComponent(Components.java:56)
...
I noticed this in Bag.java:
Code:
/**
* Constructs an empty Bag with an initial capacity of 64.
*
*/
public Bag() {
this(16);
}
Should that be this(64) instead of this(16)? I might have just added my 17th component type. It's hard to know for sure since I'm using arielsan's library and that has some component types of its own.
Although, with that said, the Bag is supposed to grow if something gets added to it, so that's not the bug. I think what happened is, I have a system that's looking for a component type that hasn't been used by any entities (yet), so the component type doesn't exist. So it never went through EntityManager.addComponent, which means it never got added to componentsByType. But when the system tried to look up this component, it ended up in EntityManager.getComponent, which crashed because I now have > 16 component types.
Maybe the answer here is "Don't do this", in which case, please let me know. My simple-minded fix would be to protect Bag.get as follows:
Code:
public E get(int index) {
if (index >= data.length)
return null;
return (E) data[index];
}
This would return null for the component lookup, which is the behavior I want in this situation.
Thanks,
Ziggy