Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  type as parameter.  (Read 377 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2014-01-12 08:59:26 »

So I'm writing this code. I would like to check if say my object is instanceof class that I put in as parameter. My code should explain itself.
1  
2  
3  
4  
      public boolean same(Item item, Class cls) {
         //this is were I don't know if it is possible to achieve.
         return item instanceof cls.getType(); // cls.getType() doesn't exist.
      }

So is there a way to convert that cls object to a type ?
I could do this other way and stuff, but this seems to be the "cleanest way" if it would be possible to achieve.
Offline Danny02
« Reply #1 - Posted 2014-01-12 09:19:47 »

Yes you can do this, but the big question here is why.
One should probably only use reflection hackery deep down in some library and not in your main business logic.

Tell us more about your problem and I promise you that we can find a way better solution.
Offline trollwarrior1
« Reply #2 - Posted 2014-01-12 09:31:35 »

Here is the code for my Inventory
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
   public static class Inventory {

      public int width, height;
      private Item[] items;

      public Inventory(int width, int height) {
         this.width = width;
         this.height = height;
         items = new Item[width * height];
      }

      public Item getItem(int x, int y) {
         if (x < 0 || x >= width || y < 0 || y >= height) return null;
         return items[x + y * width];
      }

      public void addItem(int x, int y, Item item) {
         if (x < 0 || x >= width || y < 0 || y >= height) return;
         items[x + y * width] = item;
      }

      public void addItem(Item item) {

         for (int y = 0; y < height; y++) {
            boolean br = false;

            for (int x = 0; x < width; x++) {

               Item itemInSlot = getItem(x, y);

               if (itemInSlot == null) {
                  addItem(x, y, item);
                  br = true;
                  break;
               }

            }

            if (br) break;
         }
      }

      public boolean contains(Class<Item> cls) {
         for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
               Item item = items[x+y*width];
               // if item instanceof cls.getType() return true;
            }
         }
         return false;
         
         if(inventory.contains(Axe.class)) {
            chopTree();
         }
      }
     

   }


I want to be able to check if my items array contains an item that is instanceof specified class.
So for example:
1  
2  
3  
         if(inventory.contains(Axe.class)) {
            chopTree();
         }



Ummm is it possible to pass in a ReferenceType? I could then just do item instanceof ReferenceType if I could have referenceType as an object.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2014-01-12 09:45:23 »

arg because atm I can't find of a better general solution here you go:
1  
2  
3  
4  
5  
6  
public boolean contains(Class<Item> cls) {
         for (Item i: items) {
            if(i != null && cls.isAssignableFrom(i.getClass()) return true;
         }
         return false;
}


which will work for
contains(Weapon.class)
when you have a Sword item.

On the other hand, if your special items where singletons (only one axt item in the game, no different stats. works also for special keys and so on). You could just reference check.

btw, in your addItem method you can just call return after the successful insertion, no need for the breaks and the boolean flag.
Offline trollwarrior1
« Reply #4 - Posted 2014-01-12 09:49:05 »

Thanks.

Off topic:
take a look at this speech Cheesy
kinda funny.
http://www.youtube.com/watch?v=8pTEmbeENF4
Offline Cannonball7171

Senior Newbie


Exp: 1 year


Semi-Beginner Coder


« Reply #5 - Posted 2014-01-12 11:31:06 »

Did you try using Object and then casting it?

"If you're going through Hell, keep going." -Winston Churchill
Offline trollwarrior1
« Reply #6 - Posted 2014-01-12 11:33:22 »

I need to stop looking for new posts and do some coding Cheesy

@cannonball
Could you explain yourself ?
Offline Cannonball7171

Senior Newbie


Exp: 1 year


Semi-Beginner Coder


« Reply #7 - Posted 2014-01-12 11:39:54 »

I need to stop looking for new posts and do some coding Cheesy

@cannonball
Could you explain yourself ?

Oops sorry, I misread your post Sad

"If you're going through Hell, keep going." -Winston Churchill
Offline trollwarrior1
« Reply #8 - Posted 2014-01-12 11:41:27 »

I knew something like that happened. Just trolling you as my name suggests Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!