Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
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 7552 times)
0 Members and 1 Guest are viewing this topic.
Offline macmanmatty

Junior Newbie

« 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: 174

« 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:

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: 283
Projects: 12
Exp: 12 years - 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. - 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:

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!

Pages: [1]
  ignore  |  Print  

ivj94 (583 views)
2018-03-24 14:47:39

ivj94 (48 views)
2018-03-24 14:46:31

ivj94 (382 views)
2018-03-24 14:43:53

Solater (62 views)
2018-03-17 05:04:08

nelsongames (109 views)
2018-03-05 17:56:34

Gornova (159 views)
2018-03-02 22:15:33

buddyBro (702 views)
2018-02-28 16:59:18

buddyBro (92 views)
2018-02-28 16:45:17

xxMrPHDxx (493 views)
2017-12-31 17:17:51

xxMrPHDxx (733 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

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