Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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  
  Generating Java code from text config files  (Read 1190 times)
0 Members and 1 Guest are viewing this topic.
Offline michaels0620

Junior Newbie





« Posted 2011-08-06 16:23:18 »

I'd like to know how to turn text config files into Java code.

I am working on a game and am using an Entity/Component approach where the Components are simply data containers (no processing logic). Given that they are so simple, I would like to be able to specify them in a text file and then convert them to classes.

For instance, if my text file looked like this:

1  
2  
3  
4  
5  
6  
7  
8  
Health
- maxHealth int
- currentHealth int

Position
- x float
- y float
- z float


It would create a Health Component that looked like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
import framework.*;

class Health extends Component {

   public final int maxHealth;
   public final int currentHealth;

   public Health(Entity entity, int maxHealth, int currentHealth) {
       this.entity = entity;
       this.maxHealth = maxHealth;
       this. currentHeath = currentHealth;
  }

}


It would also create an Enum of component ids for each component. This will make it easier to add new components later.

Converting the text file into something that looks like java code is not difficult, but I am not sure how to get the class instantiated along with the everything else. I have the feeling that involves using the Class Loader but while I have looked up some information on class loaders, I am a bit stuck as where to begin.

Both for the sake of performance and programming convenience, I want these to be actual classes not hashmap based properties. I don't want to end up writing code that looks like:

1  
int a = (Integer)entity.getComponent("health").getProperty("currentHealth");


Any help would be greatly appreciated.


Offline avm1979
« Reply #1 - Posted 2011-08-06 17:18:25 »

This seems like a bad idea. The java snippet would probably be faster to write than the text because of IDE support, anyway.

Supposing you're using the generated classes from some other code, you'd need to compile against the generated classes anyway, which you couldn't do if you only turned the generated code into classes at runtime.

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #2 - Posted 2011-08-06 17:34:49 »

You should only do this for hard data you don't want to hard-code (health in enemies for instance), but you can write an interpreter to read your textfile, and create the object accordingly, using the buffered-reader.

I usually use XML for defining these things, because it's nice and structured, and there is some brilliant libraries that doesn't even need configuration other than "new XStream()". It works, just like that provided that you use the standard format to  create the objects.
1  
2  
3  
4  
5  
<Enemy>
   <type>FISHMAN</type>
   <health>12</health>
   <power>5</power>
</Enemy>

Would do for creating your Enemy object, like this:
1  
2  
3  
4  
5  
public class Enemy {
   private ENEMY_TYPE type;
   private int health;
   private int power;
}

..and the variables would be set accordingly. I find that a lot easier than writing your own interpreter to read .txt's, because that usually involves a lot of Tokens and String analysation, which isn't fun in the long run and probably not worth it CPU-wise.
Also, you can wrap the objects in XML, inside an <ArrayList>-tag, to make the program return an ArrayList.
That particular library is found here: http://xstream.codehaus.org/

You can also use a format that humans can't read though, like Markus Perssons Named Binary Tag-format, which is used to save levels in Minecraft.
That just makes debugging very hard, and if you want to change some things up, you'd need an editor, which equals even more code, for silly things. At least if you're only reading/writing once. Minecraft for instance does a lot of I/O, so there it's great to use a format that's fast for the computer to process.

Cheers!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline avm1979
« Reply #3 - Posted 2011-08-06 17:41:42 »

JSON might be worth a look as an alternative to XML. I personally find it a lot faster to write by hand - it's a lot less verbose, the amount of markup is minimal.

The java library they have for parsing it is great, too, clearly written by people that had a very practical approach to the problem - which is more than I can say for most XML parsing libraries Smiley

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2011-08-06 17:58:50 »

JSON might be worth a look as an alternative to XML. I personally find it a lot faster to write by hand - it's a lot less verbose, the amount of markup is minimal.

The java library they have for parsing it is great, too, clearly written by people that had a very practical approach to the problem - which is more than I can say for most XML parsing libraries Smiley

Most of the time JSON is faster. XStream can also deal with JSON in the same manner as with XML, where you don't need configuration, which I find neat. However, if you're scared about the speed of XML I can report very large XML documents containing almost 50.000 lines of XML, loaded in less 1.8 seconds (the times my server needs to start before it's ready to accept client, and that does _alot_ of other tasks!), using the XPP3 parser, which XStream supports. You can also use kXML2 which equally is a pull-parser, that is a little bit faster than XPP3 to further speed up things.

Offline avm1979
« Reply #5 - Posted 2011-08-06 18:28:49 »

Right. For me it's not really about the loading speed, though that's a concern if it's heinously slow, but about the speed and convenience of entering the data by hand.

XStream looks good - very good, actually. I'm just not a huge fan of using reflection to fill in bean classes. It's too easy to break by doing a refactor of a variable name, and the IDE won't warn you about it.

I suppose that's where aliases come in, but if you're going to do that, that's roughly the amount of code needed to read the data in with a regular API and doing the mapping yourself - while doing some other nice things like specifying defaults etc, mapping from strings to enums, etc.

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #6 - Posted 2011-08-06 18:37:40 »

Right. For me it's not really about the loading speed, though that's a concern if it's heinously slow, but about the speed and convenience of entering the data by hand.

XStream looks good - very good, actually. I'm just not a huge fan of using reflection to fill in bean classes. It's too easy to break by doing a refactor of a variable name, and the IDE won't warn you about it.

I suppose that's where aliases come in, but if you're going to do that, that's roughly the amount of code needed to read the data in with a regular API and doing the mapping yourself - while doing some other nice things like specifying defaults etc, mapping from strings to enums, etc.

Very true about refractoring there.. I guess I just used the replaceAll-function in Notepad++ when dealing with that Tongue

Offline michaels0620

Junior Newbie





« Reply #7 - Posted 2011-08-06 19:33:12 »

Thanks for the discussion. I think for things like map data or mission editor data I will use something like either JSON or XML (probably JSON). This is slightly different in that the intent is to allow the programmer to define the Components themselves outside of code. So it isn't about loading data into existing objects so much as defining the objects themselves.

One thing I (stupidly) forgot was that the code wouldn't even compile if it used objects that did not exist until run time. Another option would be to have the program read the text file, generate Java code, and compile it to a jar file that is then imported into the game. The question is, is it worth it?

On the plus side, rather than writing n class files, one for each component, I would only have a single file to deal with. And I could automate the jar generation to make it easy to incorporate when I've made changes I want pushed to the game.

On the down side, it is seems like I may be getting snared in a weaker version of the game engine trap.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #8 - Posted 2011-08-06 20:03:25 »

Thanks for the discussion. I think for things like map data or mission editor data I will use something like either JSON or XML (probably JSON). This is slightly different in that the intent is to allow the programmer to define the Components themselves outside of code. So it isn't about loading data into existing objects so much as defining the objects themselves.

One thing I (stupidly) forgot was that the code wouldn't even compile if it used objects that did not exist until run time. Another option would be to have the program read the text file, generate Java code, and compile it to a jar file that is then imported into the game. The question is, is it worth it?

On the plus side, rather than writing n class files, one for each component, I would only have a single file to deal with. And I could automate the jar generation to make it easy to incorporate when I've made changes I want pushed to the game.

On the down side, it is seems like I may be getting snared in a weaker version of the game engine trap.

Generting new class files is not worth it. Make the objects beforehand, as you make the maps/characters beforehand, and try to make as flexible as you could possibly need. For instance, you can use enums for different types of enemies, without having to have a subclass for each type.
Only subclass if you want different behavoir, such as AI and even then can you make your entities hold a reference to the suitable AI. Smiley

Offline Cero
« Reply #9 - Posted 2011-08-06 20:41:09 »

Very true about refractoring there.. I guess I just used the replaceAll-function in Notepad++ when dealing with that Tongue

replace in ALL Documents... my favorite =D

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

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #10 - Posted 2011-08-06 20:43:02 »

Very true about refractoring there.. I guess I just used the replaceAll-function in Notepad++ when dealing with that Tongue

replace in ALL Documents... my favorite =D

Getting slightly off-topic here, but are you programming directly in notepad++? :O

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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!