Reputation: 301
I have an object that stores a series of Actors (I am using LibGDX) and currently it is storing GameObjects and LinkObjects, which both extend Actor. I need to make sure all of the GameObjects are linked if they have the same y value (I will eventually change this to a radius but this is testing.) To get the list and cast it I do this:
List<Actor> actors = this.getActors();
List<GameObject> gmObjs = (List<GameObject>)(List<?>) actors;
for(int i = 0; i<gmObjs.size(); i++) {
for(int j=i+1; j<gmObjs.size(); j++){
if(gmObjs.get(i).y == gmObjs.get(j).y && gmObjs.get(i).isLinked()==false){
this.addActor(new LinkObject((gmObjs.get(i).x+gmObjs.get(j).x)/2, gmObjs.get(i).y));
}
}
}
However it throws an error on the if statement saying LinkObject cannot be cast to a GameObject. I don't think I am casting it correctly. Is there a better way to extract only a certain subclass out of a list of objects which are are a particular generic class?
EDIT: I realized I really shouldn't be casting it. I just want a test to see if that particular Actor in the list is a GameObject, if so it puts it in another list.
Upvotes: 1
Views: 489
Reputation: 3909
You could use the instanceof
operator to check for runtime type and filter based on that. Also, judging from what you described the inheritance hierarchy to be, GameObject and LinkObject do not inherit from each other; in that case, you cannot cast one type to the other; you can only cast upwards or downwards in the inheritance hierarchy, never "sideways".
EDIT:
To extract the GameObjects into a separate list, you will have to iterate over the list of actors; you can then for each object check with instanceof
whether it's a GameObject or not. All GameObjects could then be stored in a List[GameObject] (check the code ojota provided for that). If you call that extraction method more than once, it is probably smart to simply store the list of GamObjects and have the extracting method check whether the list of actors has been updated since the last extraction of GameObjects, using a boolean flag or something.
Also, I can't tell with certainty judging from your code, but it would probably work if you added
(gmObjs.get(i) instanceof GameObject && gmObjs.get(j) instanceof GameObject)
at the very beginning of your if-clause; that way, only the GameObjects will be considered.
Another option would be to store LinkObjects and GameObjects in separate Lists in the Group object, and only operate on those lists (use both if you have to go through all Actors). That would hardly cost more memory (only the memory used to store the additional List-object, nodes are there one way or the other), would save you the time it costs to extract GameObjects, and avoid the extraction method as a potential source of bugs.
Hope that helps, just comment if that didn't answer your question :)
Upvotes: 2
Reputation: 1646
If you just want to extract the list of gameobjects existing in the actor list, you can do the following:
List<Actor> actors =this.getActors();
List<GameObject> gameObject = new ArrayList<GameObject>();
for (Actor actor : actors) {
if(actor instanceof GameObject){
gameObject.add((GameObject)actor);
}
}
Upvotes: 0