Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (731)
Games in Android Showcase (217)
games submitted by our members
Games in WIP (798)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  has map vs instaceof for determining object kind from an arraylist of arraylist  (Read 1347 times)
0 Members and 2 Guests are viewing this topic.
Offline macmanmatty

Innocent Bystander





« Posted 2017-05-19 23:59:37 »

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?
Offline ddyer
« Reply #1 - Posted 2017-05-20 18:11:43 »

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); }
     }
Offline cylab

JGO Kernel


Medals: 166



« Reply #2 - Posted 2017-05-21 08:34:47 »

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

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 247
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #3 - Posted 2017-05-24 04:44:54 »

I'm programming  an rpg where you have  many soliders  and horses and some soliders  can carry things as well as horses.

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!)

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?

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.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline klaus
« Reply #4 - Posted 2017-05-31 11:51:59 »

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!

HTH

Early access available at Asteroid Fight | Asteroid Fight on IndieDB
Pages: [1]
  ignore  |  Print  
 
 

 
Archive (336 views)
2017-04-27 17:45:51

buddyBro (535 views)
2017-04-05 03:38:00

CopyableCougar4 (978 views)
2017-03-24 15:39:42

theagentd (1014 views)
2017-03-24 15:32:08

Rule (993 views)
2017-03-19 12:43:22

Rule (973 views)
2017-03-19 12:42:17

Rule (971 views)
2017-03-19 12:36:21

theagentd (1071 views)
2017-03-16 05:07:07

theagentd (990 views)
2017-03-15 22:37:06

theagentd (760 views)
2017-03-15 22:32:18
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!