Parsing a file for game initialisation

Hello people,
I want to make a class that reads a text file that looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
<ClassName
	StartPosition="20,20"
	Color="255,255,0"
	Speed="100"
/>
<ClassName2
	Position="20,20"
	Position="20, 31"
/>
<ClassName3
	Position="6149,6971"
	Position="6180, 6990"
/>


My goal is that it reads the file once and depending on what the classname is, an object (or multiple objects) of that class are created for my game.

The problem that I have, is that every class has different variables. For example: My "player" class has a start position and a color but my "Healthpickup" class has a Health variable. So I can't think of some kind of an algorithm that would work in every case.

I made a solution already but in that case, for every variable that is in the text file, the text file is read again and again.
(I could do: GetValue(L"Player"), (L"StartPosition"))
But that is not efficient at all.
Last edited on
Is there a reason for the unusual XML-like format you have chosen? If you don't care how the data is stored, I would recommend using JSON: http://www.json.org/
[
    {"type": "Player",
        "start position": [20, 20],
        "color": [255, 255, 0],
        "speed": 100
    },
    {"type": "Portal",
        "positions":
        [
            [20, 20],
            [20, 31]
        ]
    },
    {"type": "Portal",
        "positions":
        [
            [6149, 2971],
            [6180, 6990]
        ]
    }
]
You would look at each element of the root array to find the "type" value, and then ask that class to create an instance given the JSON object.
Last edited on
I really want to use this kind of structure without using any special libraries or json.
Topic archived. No new replies allowed.