I have game objects in an array collection, but they can be different classes. I can find the object by its coordinates:
Object obb = grid.getCellContent(insertObjectX, insertObjectY);
What I need to do is to find if obb.canBeSubmerged == true and then simply move it to another array.
The problem is that I canot get a property of that object “canBeSubmerged” because compilier does not know which class I used.
I wonder if there is a quicker way to do it then to go through "obb.getClass() == myClass.class" statements for each of the possible object classes.
.
.
EDIT: based on stuckless suggestion here is a working solution:
try {
java.lang.reflect.Field f1 = obb.getClass().getField("canBeSubmerged");
java.lang.reflect.Field f2 = obb.getClass().getField("subObject");
if (f1 != null) {
canBeSubmerged = f1.getBoolean(obb);
f2.setBoolean(obb, canBeSubmerged);
}
}catch (Exception e) { }
//enter it into the submerged grid level and remove from the top grid level
if (canBeSubmerged){
grid.removeObject(insertObjectX, insertObjectY);
grid.putSubObjectInCell(insertObjectX, insertObjectY, obb);
}
If your
obbobject that contains thecanBeSubmergedproperty is always of a particular class type, such asMyOBB.class, then you can do this…========= OR ============
Using Reflection (which will be slower)