Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  How to store an object type in a variable, and use it?  (Read 893 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Wizard


Medals: 79
Projects: 4
Exp: 3 years


Radirius Games


« Posted 2013-08-13 20:34:31 »

So. I have a generated tile map. I want to make it so that when you make it, there will be a variable called "default_tile." I want to be able to put in a default object that will be copied and used. Until now, I just made a huge class that wen something like this:
1  
2  
3  
4  
5  
6  
7  
public static final int Rock=0, Sand=1;
public static Tile getTileOf(float x, float y, int type) {
if(type == Rock)
    return new Rock(x, y);
if(type == Sand)
    return new Sand(x, y);
}


But that is MESSY! What do I do instead?

Offline davedes
« Reply #1 - Posted 2013-08-13 20:43:00 »

Check out enums.

1  
2  
3  
4  
5  
enum Tile {
    Rock,
    Sand,
    etc.
};


I'm assuming you're using ints because you want to read/write from file? You can use Enum.ordinal(), but a better solution would be to just use the name:
1  
2  
3  
4  
5  
//write tile to file...
String name = Tile.Rock.name();

//parse tile from file...
Tile tile = Tile.valueOf(name);

Offline wessles

JGO Wizard


Medals: 79
Projects: 4
Exp: 3 years


Radirius Games


« Reply #2 - Posted 2013-08-13 20:45:40 »

Wait, you just blew my mind. Enums can be used for something useful? HOLY CRAP! I always thought that it was a reason to not use public static final int, and stuffs. But Ill look it up.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #3 - Posted 2013-08-13 20:50:54 »

And the even more (java only (so far i know)) awesome part, they can have propertys and functions:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public enum ToolType {
    SHOVEL(1),
    PICKAXE(2);
   
    private static HashMap<Byte, ToolType> idlist = new HashMap<>();
    static{
        for(ToolType m : ToolType.values()){
            idlist.put(m.getID(), m);
        }
    }
    public static ToolType get(byte id){ return idlist.containsKey(id) ? idlist.get(id) : ToolType.SHOVEL; }
   
   
    private final byte id;
    private ToolType(int id){
        this.id = (byte)id;
    }
   
    public byte getID(){ return id; }
}
Offline wessles

JGO Wizard


Medals: 79
Projects: 4
Exp: 3 years


Radirius Games


« Reply #4 - Posted 2013-08-13 20:53:56 »

What is a hashmap?

Offline davedes
« Reply #5 - Posted 2013-08-13 20:55:15 »

You can do a lot of cool things with enums. For example, implement an interface:

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  
public interface ButtonListener {
   public void clicked(GameContext context);
}

protected enum MenuActions implements ButtonListener {
   
   PLAY("Play") {
      public void clicked(GameContext context) {
         //States is another enum!
         context.enterState(States.GAME);
      }
   },

   HELP("Help") {
      public void clicked(GameContext context) {
         context.enterState(States.HELP);
      }
   },
   
   EXIT("Exit") {
      public void clicked(GameContext context) {
         context.exit();
      }
   };
   
   public final String text;
   
   MenuActions(String text) {
      this.text = text;
   }
}



Now your GUI code is decoupled from the actual content of your GUI. In other words; when you want to change the menu text or button actions, you don't need to touch your GUI code. Typical usage:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
//iterate through each button in the menu buttons enum
for (final MenuActions a : MenuActions.values()) {
   //use the text we specified in the num
   TextButton btn = new TextButton(a.text, skin);
   
   //add the listener specified in the enum...
   btn.addListener(new ClickListener() {
      public void clicked(InputEvent e, float x, float y) {
         a.clicked(context);
      }
   });
   
   //add the button
   table.add(btn);
   
   //then move down a row
   table.row();
}


Full post:
http://www.java-gaming.org/index.php?topic=29415.0

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 298
Exp: 5 years



« Reply #6 - Posted 2013-08-13 21:15:56 »

You can do a lot of cool things with enums. For example, implement an interface:
<snip>

You ninja'd me with recommending enums, but now I'm glad you did! That is actually quite useful and I wouldn't have though of it myself. I'll probably start using that technique now. Thanks!
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #7 - Posted 2013-08-13 21:22:21 »

What is a hashmap?

It does not have anything to do with enums, hashmaps are just genric java stuff.
An hashmap allows you to look for objects really fast, example:


1  
2  
3  
4  
HashMap<Integer, Person> people = new HashMap<Integer, Person>();
people.put(4564534, new Person("Person1"));
people.put(2342423, new Person("Person2"));
people.put(2135344, new Person("Person3"));


Now imagine this list is very long (100+ / 10000+ maybe).
When your looking for an person in a array (with his id), you need to loop trough the whole array.
With an hashmap, you dont have to!

1  
2  
3  
4  
5  
if(people.containsKey(id)){
    return people.get(id);
}else{
    //Person does not exist
}
Offline wessles

JGO Wizard


Medals: 79
Projects: 4
Exp: 3 years


Radirius Games


« Reply #8 - Posted 2013-08-13 23:24:27 »

Wait a minute. What do I do once I have all the tiles and their methods made? How do I put a type into a variable, so that I can later say that it is the default tile, and that I want it made?

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 298
Exp: 5 years



« Reply #9 - Posted 2013-08-14 00:13:40 »

There really isn't much (optimization?) of your example you can do (that I can think of):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public static enum Type {
     Rock, Sand;
}
   
public static Tile getTileOf(float x, float y, Type type) {
     if(type == Type.Rock)
           return new Rock(x, y);
     if(type == Type.Sand)
          return new Sand(x, y);
}


This at least makes it so that you can't pass in an invalid Type. (Like you could accidentally with ints)

Also there is Reflection: (there's probably a better way of using generics, I'm not too practiced with them)
1  
2  
3  
4  
5  
6  
public static Tile getTile(float x, float y, Class<? extends Tile> type) {
   Tile tile = type.newInstance();
   tile.setX(x);
   tile.setY(y);
   return tile;
}


You would use it like so:
Tile t = getTile(2, 6, Rock.class);


Also take a look at AssetManager.java from libGDX if you have it, uses similar technique (probably done better too).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #10 - Posted 2013-08-14 00:42:16 »

What is a hashmap?
No offense and stuff, and I wasn't any better back then: But you should really learn some more of the basics before making a game

Offline davedes
« Reply #11 - Posted 2013-08-14 00:47:04 »

Best not to use reflection. Generally it is poor practice / code smell when used in this fashion. Better to use object-oriented patterns, or plain old enums if you know your types are fixed. Here is a related design pattern:
http://alvinalexander.com/java/java-command-design-pattern-in-java-examples

If you have very few and very simple tiles, then enums work fine like I posted earlier. You don't need reflection to get the enum Type. Just use
Type.valueOf(String)
or
Type.values()[index]
.

Tile type also should not have any information about x/y coordinates. Why? Because a tile is just a type; either rock, sand, or grass. It isn't an entity on the screen.

In most cases you will want to store more than just "tile type" along with a tile. For example; see the following tile map:


Most of it falls under "Sand" although it can't all point to the same tile type. So your options here are to use a tile type for each tile (SandDirtTopLeft, SandDirtTopCenter, etc) -- but that gets ugly real quick. Another option is to use "traits" instead of focusing strictly on tile types. So, the sand tiles have
Trait.Walkable
, the objects might have
Trait.Blocked
, and some objects might have mutliple traights, like
Trait.SignPost
.

It quickly becomes pretty daunting to work with tiled maps, which is why it's almost always a better idea to use pre-made solutions rather than trying to reinvent the wheel. For example:
http://www.mapeditor.org/

LibGDX supports the TMX format, so it's a piece of cake to use.

Offline wessles

JGO Wizard


Medals: 79
Projects: 4
Exp: 3 years


Radirius Games


« Reply #12 - Posted 2013-08-14 01:01:29 »

What is a hashmap?
No offense and stuff, and I wasn't any better back then: But you should really learn some more of the basics before making a game
Sorry, but I have never had to use it. No offense taken, but still, I did not need to know what that was. Basically it is just an arraylist, but with ids. Chillax!
Also, I am making this game to learn how to make a game. Not any book in the world could teach better than experience.

Offline wessles

JGO Wizard


Medals: 79
Projects: 4
Exp: 3 years


Radirius Games


« Reply #13 - Posted 2013-08-14 01:05:20 »

Thanks! That was JUST What I was looking for! Now I won't have to take weeks to change all of the code to form another less cool looking way. Thanks!

But:

Also take a look at AssetManager.java from libGDX if you have it, uses similar technique (probably done better too).

I am surprisingly 'meh' about libGDX. I mean, I would rather make my own awesome engine, that I understand completely, and will never have to troubleshoot on, than use an even awesome-er one, that will maybe be just a fad in a year, of which will take a year to fully understand. Plus, you get to say that you made an engine from scratch, and that is fun to say with truth.

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 298
Exp: 5 years



« Reply #14 - Posted 2013-08-14 01:15:39 »

I am surprisingly 'meh' about libGDX. I mean, I would rather make my own awesome engine, that I understand completely, and will never have to troubleshoot on, than use an even awesome-er one, that will maybe be just a fad in a year, of which will take a year to fully understand. Plus, you get to say that you made an engine from scratch, and that is fun to say with truth.

Ah, but it will take you many years to make your own that is up to the same standard. Some of them spent troubleshooting.  Pointing
Also, libGDX isn't a fad by any means of the word.
Offline davedes
« Reply #15 - Posted 2013-08-14 01:16:14 »

Quote
I am surprisingly 'meh' about libGDX. I mean, I would rather make my own awesome engine, that I understand completely, and will never have to troubleshoot on, than use an even awesome-er one, that will maybe be just a fad in a year, of which will take a year to fully understand. Plus, you get to say that you made an engine from scratch, and that is fun to say with truth.
It doesn't take a long time to learn LibGDX. Maybe a month or two. Keep in mind LibGDX has been in development for years and is made up of several million lines of code.

For example; why write your own TMX loader when you can just use LibGDX?
LibGDX TMX Loader

Rather than spending your time writing a lot of boilerplate code, IMHO your time would be better spent learning higher level OpenGL concepts, like shaders and meshes:
https://github.com/mattdesl/lwjgl-basics/wiki/Shaders
https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Meshes

Offline wessles

JGO Wizard


Medals: 79
Projects: 4
Exp: 3 years


Radirius Games


« Reply #16 - Posted 2013-08-14 01:18:34 »

Sorry guys. I just have an annoying habit of trying to do stuffs on my own. But I still will use libGDX, just not for major projects. I like to be able to learn everything, from the ground up. Kind of an OCD, if you will. Sorry, but I am sold on my own idea.

EDIT:
Also, I looked at TMX, and why would I ever even make that? I would just make my own tile map editor. It isn't that hard. Just have to sit down and do it.

EDIT AGAIN:
And, I never said that my engine would be as good, or even amount to it. I just prefer to use my own work.

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 298
Exp: 5 years



« Reply #17 - Posted 2013-08-14 01:23:05 »

I just have an annoying habit of trying to do stuffs on my own.
I just prefer to use my own work.

Pretty sure you aren't a programmer if you don't think like this, at least a bit.  Wink
Offline davedes
« Reply #18 - Posted 2013-08-14 01:23:39 »

If you want a good tile map editor, with even a fraction of the features of Tiled, then it will most likely take a lot longer to develop than it will to write a TMX parser.

Anyways; good luck. I'm sure in time you will realize that building an engine is tedious. Or, maybe after a number of years of development, your engine will rival LibGDX. Smiley

good programmers write good code; great programmers steal great code

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 298
Exp: 5 years



« Reply #19 - Posted 2013-08-14 01:25:55 »

good programmers write good code; great programmers steal great code

Indeed. To build on the shoulders of giants.
Offline wessles

JGO Wizard


Medals: 79
Projects: 4
Exp: 3 years


Radirius Games


« Reply #20 - Posted 2013-08-14 01:37:51 »

Anyways; good luck. I'm sure in time you will realize that building an engine is tedious. Or, maybe after a number of years of development, your engine will rival LibGDX. Smiley

good programmers write good code; great programmers steal great code

I am already building an engine. I have found that making it as I go is good. And this engine will only be used by me and whoever else understands it. I am sure that badlogicgames worked hard, with intent to make it usable by everyone. This is only meant for me, and therefor will be easily used by me.

Also, who said that I am not stealing great code? Lwjgl? JInput? (small bits of)slick2d?

Offline SwampChicken
« Reply #21 - Posted 2013-08-14 14:38:59 »

good programmers write good code; great programmers steal great code

You have to be better than good to be able to even spot great code...
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.

Mr.CodeIt (10 views)
2014-12-27 04:03:04

TheDudeFromCI (15 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02

SHC (96 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

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