What to use for config files?

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 :D).

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?

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.

I am using json and had no problems yet :slight_smile:

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.

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:

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

Read it:

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.

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

You might want to give this thingy a shot :smiley:

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.

+1 for Properties, that’s the way I learned to do setting files in lecture. Really easy to use! :slight_smile:
That Preferences thing looks also quite interesting. :wink:

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?)


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();}
}