Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  What to use for config files?  (Read 590 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771
« Posted 2014-03-11 13:50:30 »

I am still working on my level loader for my game currently called Code 15, I have the level loading in efficiently and quickly (even with 1million+ tiles Cheesy).

However I am only wanting to do this to load in "static" parts of the level, such as walls, doors, basically things that the player can not really do anything with.

I want to load in items, enemies and other things using a config file.

Since I am building the levels one by one, this seems like a good choice since the only thing that can really vary with the things I want to load would be maybe hitpoints or something similiar.

I was going to use Json to simply serialize my final hardcoded level layout, then remove all the code and use a json.read call to init my arrays of items and what not, is this a good approach?


"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Varkas
« Reply #1 - Posted 2014-03-11 13:54:40 »

Usually I just use text files, if I want to appear more professional I'm using XML. But actually, unless you want to share the data with other projects, just use whatever is the most easy way for you.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Phibedy

Senior Member


Medals: 8



« Reply #2 - Posted 2014-03-11 13:58:57 »

I am using json and had no problems yet Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gibbo3771
« Reply #3 - Posted 2014-03-11 13:59:21 »

Usually I just use text files, if I want to appear more professional I'm using XML. But actually, unless you want to share the data with other projects, just use whatever is the most easy way for you.

Not going to lie, not even sure how to start with a text file. How would it look? Any guides?

All I want to do is store a position, a few fields and that is pretty much it. I was thinking Json for readability but I would love to learn more about writing files that I can read from in my own way.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Varkas
« Reply #4 - Posted 2014-03-11 14:05:35 »

It looks to me that you were born in a much higher lvel world than I was, and won't be happy with text files ... but here, how to store and read a location (x/y pair)

Store a location:

1  
2  
Writer writer = new FileWriter("filename.txt");
writer.write("" + x + " " +  y");


Read it:

1  
2  
3  
4  
5  
BufferedReader reader = new BufferedReader(new FileReader("filename.txt");
String line = reader.readLine();
String [] parts = line.split(" ");
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]);


Code typed from memory, might have bugs.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline junkdog
« Reply #5 - Posted 2014-03-11 14:26:13 »

Json is what I normally prefer - easy to read, easy to debug, easy to use. Nate's JsonBeans lib is very nice.

artemis-odb: bugfixing and performance optimized fork of artemis ES
Offline trollwarrior1
« Reply #6 - Posted 2014-03-11 15:01:08 »

You might want to give this thingy a shot Cheesy

http://www.java-gaming.org/topics/parser101/32002/view.html
Offline BurntPizza
« Reply #7 - Posted 2014-03-11 20:57:17 »

If you wan to use the standard library, Properties is a (somewhat old, but usable) way to save key-value pairs in files. You use it as a hashmap, and can store it to a file via a stream, or read a hashmap in from a stream. It also supports xml. Properties is good for using as a simple config file.

Edit: Also Preferences is probably even better than properties, check it out.

Otherwise, Kyro is probably overkill, but can serialize Java objects (i.e. a hashmap of settings) with an easy API. If you plan on using actual serialization, Kyro is fast and powerful.


Or you could bang out a simple line-by-line text file reader for config or use any of the numerous JSON libs for serialization.
Offline Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #8 - Posted 2014-03-11 21:12:58 »

If you wan to use the standard library, Properties is a (somewhat old, but usable) way to save key-value pairs in files. You use it as a hashmap, and can store it to a file via a stream, or read a hashmap in from a stream. It also supports xml. Properties is good for using as a simple config file.

Edit: Also Preferences is probably even better than properties, check it out.

Otherwise, Kyro is probably overkill, but can serialize Java objects (i.e. a hashmap of settings) with an easy API. If you plan on using actual serialization, Kyro is fast and powerful.


Or you could bang out a simple line-by-line text file reader for config or use any of the numerous JSON libs for serialization.

Beat me to the punch, I was going to suggest using properties. Thats what I use for my settings file. It's simple and straightforward, easy to load/edit/create/reset. I have a pretty straight forward class that loads up settings out of a .properties file, and if the file doesn't exist, it creates one with "default" settings.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline SilverTiger

Junior Member


Medals: 4
Exp: 2 years


がんばってください!


« Reply #9 - Posted 2014-03-11 21:15:05 »

If you wan to use the standard library, Properties is a (somewhat old, but usable) way to save key-value pairs in files. You use it as a hashmap, and can store it to a file via a stream, or read a hashmap in from a stream. It also supports xml. Properties is good for using as a simple config file.

Edit: Also Preferences is probably even better than properties, check it out.

+1 for Properties, that's the way I learned to do setting files in lecture. Really easy to use!  Smiley
That Preferences thing looks also quite interesting. Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #10 - Posted 2014-03-11 21:16:46 »

Here's my SettingsParser.class if you want to use it as a template, it has a ton of try/catches to check if settings exist and recreate ones that don't. It's a pretty solid foundation to build yours off of.

It's also setup to use getSettingName() methods so your other class files can fetch settings.

Should be noted this is basically an abstract class though, it's basically 100% static and accessed directly from other classes (yeah I know, bad OOP, but really it's a settings class, does it need to be an object?)

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  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
package core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.lwjgl.input.Keyboard;

public class SettingsParser{
   private static int KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_INVENTORY, KEY_CHARACTER, KEY_MAP, KEY_REPUTATION, KEY_USE_OBJECT, KEY_MAIN_MENU, KEY_DEBUG, KEY_FULL_SCREEN;
   private static boolean FULL_SCREEN = false;
   private static Properties properties = new Properties();
   private static InputStream in = null;
   private static File settings = new File("settings.properties");
   
   public SettingsParser(){
      init();
   }  
   
   public static void init(){
      try {
         in = new FileInputStream(settings);
         properties.load(in);
         try {
            KEY_UP = Keyboard.getKeyIndex(properties.getProperty("key_up").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_up", "w");
            KEY_UP = Keyboard.getKeyIndex(properties.getProperty("key_up").toUpperCase());
         }
         try {
            KEY_DOWN = Keyboard.getKeyIndex(properties.getProperty("key_down").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_down", "s");
            KEY_DOWN = Keyboard.getKeyIndex(properties.getProperty("key_down").toUpperCase());
         }
         try {
            KEY_LEFT = Keyboard.getKeyIndex(properties.getProperty("key_left").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_left", "a");
            KEY_LEFT = Keyboard.getKeyIndex(properties.getProperty("key_left").toUpperCase());
         }
         try {
            KEY_RIGHT = Keyboard.getKeyIndex(properties.getProperty("key_right").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_right", "d");
            KEY_RIGHT = Keyboard.getKeyIndex(properties.getProperty("key_right").toUpperCase());
         }
         try {
            KEY_INVENTORY = Keyboard.getKeyIndex(properties.getProperty("key_inventory").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_inventory", "i");
            KEY_INVENTORY = Keyboard.getKeyIndex(properties.getProperty("key_inventory").toUpperCase());
         }
         try {
            KEY_CHARACTER = Keyboard.getKeyIndex(properties.getProperty("key_character").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_character", "c");
            KEY_CHARACTER = Keyboard.getKeyIndex(properties.getProperty("key_character").toUpperCase());
         }
         try {
            KEY_MAP = Keyboard.getKeyIndex(properties.getProperty("key_map").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_map", "m");
            KEY_MAP = Keyboard.getKeyIndex(properties.getProperty("key_map").toUpperCase());
         }
         try {
            KEY_REPUTATION = Keyboard.getKeyIndex(properties.getProperty("key_reputation").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_reputation", "r");
            KEY_REPUTATION = Keyboard.getKeyIndex(properties.getProperty("key_reputation").toUpperCase());
         }
         try {
            KEY_USE_OBJECT = Keyboard.getKeyIndex(properties.getProperty("key_useObject").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_useObject", "space");
            KEY_USE_OBJECT = Keyboard.getKeyIndex(properties.getProperty("key_useObject").toUpperCase());
         }
         try {
            KEY_MAIN_MENU = Keyboard.getKeyIndex(properties.getProperty("key_mainMenu").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_mainMenu", "escape");
            KEY_MAIN_MENU = Keyboard.getKeyIndex(properties.getProperty("key_mainMenu").toUpperCase());
         }
         try {
            KEY_DEBUG = Keyboard.getKeyIndex(properties.getProperty("key_debug").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_debug", "f3");
            KEY_DEBUG = Keyboard.getKeyIndex(properties.getProperty("key_debug").toUpperCase());
         }
         try {
            KEY_FULL_SCREEN = Keyboard.getKeyIndex(properties.getProperty("key_fullScreen").toUpperCase());
         } catch (NullPointerException npe) {
            properties.setProperty("key_fullScreen", "f12");
            KEY_FULL_SCREEN = Keyboard.getKeyIndex(properties.getProperty("key_fullScreen").toUpperCase());
         }
         try {
            if (properties.getProperty("fullScreen").toUpperCase().equals("TRUE")){
               FULL_SCREEN = true;
            }else{
               FULL_SCREEN = false;
            }
         } catch (NullPointerException npe) {
            properties.setProperty("fullScreen", "false");
            FULL_SCREEN = false;
         }
         FileOutputStream out = new FileOutputStream(settings);
         properties.store(out, "Settings");
         in.close();
         out.close();
      } catch (IOException ex) {
         try {
            properties.setProperty("key_up", "w");
            properties.setProperty("key_down", "s");
            properties.setProperty("key_left", "a");
            properties.setProperty("key_right", "d");
            properties.setProperty("key_inventory", "i");
            properties.setProperty("key_character", "c");
            properties.setProperty("key_map", "m");
            properties.setProperty("key_reputation", "r");
            properties.setProperty("key_useObject", "space");
            properties.setProperty("key_mainMenu", "escape");
            properties.setProperty("key_debug", "f3");
            properties.setProperty("key_fullScreen", "f12");
            properties.setProperty("fullScreen", "false");
            FileOutputStream out = new FileOutputStream(settings);
            properties.store(out, "Settings");
            out.close();
            init();
         } catch (IOException ex2) {
            ex2.printStackTrace();
         } finally {
            if (in != null) {
               try {
                  in.close();
               } catch (IOException e) {
                  e.printStackTrace();
               }
            }
         }
      } finally {
         if (in != null) {
            try {
               in.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
   
   public static void setFullScreen(Boolean b){
      try {
         in = new FileInputStream(settings);
         properties.load(in);
         if (b){
            properties.setProperty("fullScreen", "TRUE");
         }else{
            properties.setProperty("fullScreen", "FALSE");
         }
         FileOutputStream out = new FileOutputStream(settings);
         properties.store(out, "Settings");
         in.close();
         out.close();
         init();
      } catch (IOException ex) {
         ex.printStackTrace();
      } finally {
         if (in != null) {
            try {
               in.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }

   public static void settingsReset(){
       settings.delete();
       init();
   }
   
   public static void setSetting(String setting, String setTo){
      try {
         in = new FileInputStream(settings);
         properties.load(in);
         properties.setProperty(setting, setTo);
         FileOutputStream out = new FileOutputStream(settings);
         properties.store(out, "Settings");
         in.close();
         out.close();
         init();
      } catch (IOException ex) {
         ex.printStackTrace();
      } finally {
         if (in != null) {
            try {
               in.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }

   public static boolean getFullScreen(){return FULL_SCREEN;}
   public static int getKeyUp(){return KEY_UP;}
   public static int getKeyDown(){return KEY_DOWN;}
   public static int getKeyLeft(){return KEY_LEFT;}
   public static int getKeyRight(){return KEY_RIGHT;}
   public static int getKeyInventory(){return KEY_INVENTORY;}
   public static int getKeyCharacter(){return KEY_CHARACTER;}
   public static int getKeyMap(){return KEY_MAP;}
   public static int getKeyReputation(){return KEY_REPUTATION;}
   public static int getKeyUseObject(){return KEY_USE_OBJECT;}
   public static int getKeyMainMenu(){return KEY_MAIN_MENU;}
   public static int getKeyDebug(){return KEY_DEBUG;}
   public static int getKeyFullScreen(){return KEY_FULL_SCREEN;}
   public static String getProperty(String p){return properties.getProperty(p).toUpperCase();}
}

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

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

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

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

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

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

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

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

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

BurntPizza (38 views)
2014-08-08 02:01:56
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!