2010
10.19

More parser dynamics

Ok, support for dynamic (or auto-declaring) structs has just been added to svn libtcod. Now parsing a simple config file requires only one line of code. Example :

config.txt

struct video {
    bool fullscreen=false
    int consoleWidth=80
    int consoleHeight=50
}
struct input {
    struct keyboard {
        char up='W'
        char down='S'
        char left='A'
        char right='D'
    }
}

How to parse this file :

TCODParser parser;
parser.run("config.txt");

How to read values :

parser.getBoolProperty("video.fullscreen")
parser.getCharProperty("input.keyboard.up")

8 comments so far

Add Your Comment
  1. Holly Molly! This changes everything 😀 There’s always a bit of a setup involved in creating/changing the layout of your config files, but now you can just knock yourself out adding new properties to the mix, and loading them just like that! Not that specifying a schema (as we did previously) is wrong, it’s just that this lowers friction a lot when adding or changing properties, which makes it much more likely that we offload data to the config files. Also, you can always switch to the more explicit method, which includes the benefit of proper validation, when releasing your game in the wild 🙂

  2. Apparently I’m spam *insert Monty Python sketch* so I’ll try again. (newer post this time)

    How does the “dice” type work? I can see how it is constructed from the docs, but I can’t see how to “use” it within programs – it looks like a datatype, but I can’t see anything more than that… Can you call a routine to get a result back, or can you get the individual bits back somehow (in order to do your own stuff with it)?

    Ta muchly!

  3. No, indeed it’s only a datatype. It only parse a string like “3x3d20+3” and put values in the TCOD_dice_t struct. It’s up to you to use those values and some RNG to get a random value. Indeed, adding that feature in libtcod would make sense…

  4. Ooh! Me, me, me!!! I want to add it!

  5. Dice, as well as random numbers? You spoil me Sir!

    I haven’t tried the parser yet, so I didn’t know how the “custom” types (dice in particular) returned… To be honest, I’ve found it a little scary so far! (Plus, I have a tendency to write my die notations with capital Ds… Does it deal with those?)

  6. yep. Both d and D are supported. A quick dive in the code gives me this syntax :
    [<m>x]<n>(D|d)<f>[<a>]

    m : multiplier
    n : number of rolls
    f : number of faces
    a : additive (positive or negative)

  7. This should actually be:
    [(x|*)](D|d)[<a>]
    I find it more intuitive to write 2*1d8+1 than 2x1d8+1. Don’t you?

  8. ok, it now support both 😀