Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm building a game where enemies come in waves. I want to create a file where I can define data about the waves (# of enemies, spawn times, speeds, etc.). I come from a background in iOS and would normally use something like a plist. What would be the best way to do something like this in Java and Android?

share|improve this question
    
XML files could work well for you. –  Alex M. Feb 24 '13 at 2:59
add comment

2 Answers 2

up vote 4 down vote accepted

There's no single "best" way to do this, it depends on the needs of your game. Here are some options:

Hard Coded

Both the easiest and probably least flexible approach. No reason you can't just make a static class with static fields for each of the levels. Actually, this isn't a bad way to prototype, so you can move it into some other approach later. If you're going to prototype this way, do yourself a favor and make the fields private (accessible through a get() method) so you can just refactor those methods into pulling from the proper source later.

XML

This one's my vote. If you're going the XML route, there are a number of available libraries you can use to help parse the XML. (Here's one called XMLPullParser.) When you're storing flat files in android you'll have to decide whether to use internal or external storage.

  • Internal: "You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed." (Source and more detail on how to implement here)

  • External: "Every Android-compatible device supports a shared 'external storage' that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer." (Source and more info here.)

In your case, you probably want to use internal storage, but if you want to allow players to "mod" the game or create their own levels, you could store it externally.

A note about reading from hard files: You're going to need to do this asynchronously from your main thread. Read up on AsyncTask for implementation details.

JSON

As an alternative to XML, you could store the objects as JSON objects. Storing as a JSON is very similar to what you're used to (i.e., storing in plist). Both describe storage of serialized objects.

Most of what I just said about XML above applies here as it's another flat file approach. For JSON the best tip I can give is check out GSON, it's super-handy.

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

DATABASE

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. It's built into every android device. The libraries you'll need are available in Android's SQLite Package. Google's developer docs can help you with this as well. A fantastic tutorial on this from vogella.com is available here.

PULL FROM A SERVER

This may be overkill, but if you already have some online component to your game, you might consider pulling the levels down from the server so you can update them without any update on the client side. I recommend and use KryoNet for rolling one's own server in java. Course, this just backs up the question of how the levels are stored on the server side, but at that point we're not talking android specific anymore.

share|improve this answer
add comment

You most definitely want to use something like XML or JSON if a plist if what you're familiar with. Take a look at some of the available libraries you can use to parse either and store your data like that. A simple XML file might look like this:

<Data>
    <NumberOfEnemies>20</NumberOfEnemies>
    <SpawnSpeed>60</SpawnSpeed>
</Data>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.