Skip to content

Configuration files

Mark Knol edited this page Jun 25, 2014 · 7 revisions
### Working with XML, JSON or INI files

If you want to store your levels or settings in a file, you have multiple options to grab them. All you need is to use the getFile() on your assetpack, which returns the file content as String.

📄 Working with XML files

Let's say you have this settings.xml in your assetpack.

<?xml version="1.0" encoding="UTF-8"?>
<settings>
   <game>
     <name>My Game</name>
     <version>3.1</version>
   </game>
</settings>

Then you can access the info like this:

var myXML = new Fast(Xml.parse(pack.getFile("settings.xml").toString()).firstElement());	
trace(myXML.node.game.node.name.innerData); // output : My Game
trace(myXML.node.game.node.version.innerData); // output : 3.1

Read more on XML or xml.Fast on haxe.org.

📄 Working with JSON files

Let's say you have this settings.json in your assetpack.

{
  "settings": {
      "game": {
          "name": "My Game",
          "version": 3.1
      }
   }
}

Then you can access the info like this:

var myJson:Dynamic = Json.parse(pack.getFile("settings.json").toString());
trace(myJson.settings.game.name);  // output : My Game
trace(myJson.settings.game.version); // output : 3.1
📄 Working with INI configuration files

Flambe has a .ini file parser. Let's say you have this settings.ini in your assetpack.

[game]
name="My Game"
version=3.1

Then you can access the info like this. A path is a section and key name separated by a dot. A path without a dot is assumed to be in the main section.

var myConfig:MessageBundle = MessageBundle.parse(pack.getFile("settings.ini").toString());
trace(myConfig.get("game.name")); // output : My Game
trace(myConfig.get("game.version")); // output : 3.1

Want multi-language configuration? Read Multi-language assets

Clone this wiki locally