has map vs instaceof for determining object kind from an arraylist of arraylist

I’m programming an rpg where you have many soliders and horses and some soliders can carry things as well as horses. They are stored in a player class and are stored arraylists of horses and soliders . Each man will have an array list of items that could be weapons, armor, tents potions wands , other stuff ect. Each item has a weight. Each man and horse has max weight he can carry. Every usable Object except food water and gold extends the abstract item class other extend other abstract classes such as weapons or armor or wands, potions ect. If I wanted to remove an item would hashmapping each class be better than checking instanceof class with a for loop to find the item?

Use a common ancestor class, or interfaces, to abstract the type of objects. Instead of
using instanceof use a predicate method that is implemented differently in different top
level classes.

     class Base {
      public boolean isSoldier() { return(false); }
     class Soldier extends Base { 
      public boolean isSoldier() { return(true); }

I would go with hashmapping. I don’t like cluttering a baseclass with flags and also don’t see any conceptional difference between entitiy.isSoldier() and entity instanceof Soldier.

This might alsobe an interesting read for you: http://www.java-gaming.org/topics/getting-rid-of/37987/view.html

So wait… the soldiers can carry horses?

(As a side note, you might want to try using proper punctuation. It would make your post much easier to read!)

Better in what sense? Chances are you aren’t talking about thousands of items, so you probably won’t be able to notice the difference. Does a particular approach make more sense in your own brain? Go with that. Not sure which one makes more sense to you? Try putting together a little example program that tests out each approach. Which one do you like better?

In programming, there is no single correct way to do anything. So you have to figure out what works best for YOU. We can’t really help you with that. The only way to figure that stuff out is by trying things out for yourself.

That being said, I’m not sure either one of your approaches makes sense in my brain. Why do you need to do either one? Presumably you have an inventory that a player is choosing from. The player is selecting an instance, or an index, or a key, or something. Why can’t you just use that?

For example, if the inventory is just 10 boxes on the screen, then maybe that’s an array. If the player clicks on one of the boxes, then you know which index to remove. Or if it’s an inventory of named objects, then you could use a Map from names to instances. But I don’t think a Map that contains Classes makes a ton of sense. The Map would contain instances.

Honestly it sounds like you might want to try implementing something a little simpler first.

What was for me a mind-blowing insight into game development and made the type of complex game that we are working now possible was an architectural style called “entity component system”.

I recommend reading into this. There are already libraries for this architecture: https://github.com/libgdx/ashley/wiki/How-to-use-Ashley

With such a system in place, you detect if an object is of a type “Soldier” (as in your example) if the entity has a corresponding component - let’s call it a SoldierComponent. :wink:

So you would then in e.g. an AttackSystem call something along the lines of entity.hasComponent(SoldierComponent.class); // <-- example code only to show the idea behind it!