Best way to load in level information?

Hi all

I was thinking that an XML document might suit this purpose pretty well as my levels need the following information: Row, columns, target number to reach, set of numbers for each column that will create an amount (Aim of the game is to get all the columns to equal the same amount)

I was thinking the file might look something like this?


<Level id="1">
      <LevelDetails target="7" rows="3", columns="3">
          <ColumnOne> 3, 4, 12 </ColumnOne>
          <ColumnTwo> 2, 5, 3 </ColumnTwo>
          <ColumnOne> 6, 1, 4 </ColumnOne>
      </LevelDetails>
</Level>

This way I can store the target, generate the sprites for the rows and the columns and then put the values inside each column?

I guess what you just did was designing the internal structural model of what a “level” would have to be.
And you communicated that model to us in terms of its XML representation.

That’s fine if you want validation of whether your model suits the needs of your application/game logic (which I cannot really tell much about without knowing more about the actual use).
But it does not necessitate the use of XML as an externalized serialization format of that model.
You can use any kind of serialization, such as a custom binary format, YAML or even Java’s binary serialization also used by RMI.

XML would be suitable if you want some human to be able to edit the plain XML files and/or if you want to integrate external software systems that should be able to read and maybe write your level without them needing a new custom serialization process (they can just use XML as information exchange format).
You only would need to describe the schema of your level XML file via some other XML file (XML Schema) so that external programs would at least know the existence and format of the elements and attributes of your XML.

Another popular reason for using XML is because integrating it in your program is quite easy and does not take long.
For example, just use XStream on a simple Java POJO and you’re done. :slight_smile:

Thanks for the in-depth reply, KaiHH :slight_smile:

So XML might not be the best way of storing information about the level as I don’t really want people editing the files.

I’m mainly just looking for a way that I can store information about each level and load it in, rather than having to hardcode everything (I can just have a class called Level and then read a file, and set it’s variables equal to what I need). This means I need a different class for each level

You’re welcome! :slight_smile:

I’d say for the start XML should suit you just fine.

Define your model as Java classes and then let XStream just write the data out.
XStream (such as every other Java serialization library) uses the Java Reflection API under the hood to iterate over all fields of your class and serialize them.
If you read back the written XML file via XStream you get the exact same information back as an instance of your Level.

As for the confidentiality aspect of your data security: in general it is always possible to reverse engineer your data format.
You can however provide base-level security by data encryption, which means you could still write XML but then have it encrypted by some Java encryption mechanism such as via CypherOutputStream.

This however is also not totally secure, since people could in theory find the way you are encrypting things by looking at the decompiled source code.
But this would be the easiest to implement.

A somewhat weaker but also easy to implement approach would be to just use Java’s binary serialization via ObjectOutputStream.
But this approach has the drawback of not providing easy model versioning capability, should you once have multiple different versions of a Level model format.
XML is better at this, since you can always add more fields to your class and old versions would just ignore these fields.