Storing Data A Note About Creating Games Why
Storing Data
A Note About Creating Games
Why do you want to store data? 1. Data files 2. Configuration files
Configuration files Pros: • Pulls crap like strings and constants out of your code • Lets the same code run with different configurations at runtime • Others can edit the configuration files themselves…sometimes the configuration files are a programming language unto themselves • Centralizes config stuff Cons: • You now must consider file management
Example Config File Application: Internationalized Applications • Instead of JButton my. Button = new JButton(“Press Here to Continue”); JButton my. Button = new JButton(String. Config. get. String. For(“PRESS_HERE”));
How do I store data? 1. The right answer is almost always not write a file format yourself 2. Know and use the right tool for the job: 1. 2. 3. 4. 5. Write the file format yourself (flat file) Serializing XML JSON Databases
Flat File Pros: • Pretty Easy • Requires no fancy libraries whatsoever • You have total control, so you can deal with space/performance constraints directly (though this is not usually particularly easy) Cons: • Parsing is notoriously error prone • If you don’t deal with speed, it’s slow • Concurrency and half written files are an issue
Serializing • Pros – Super duper easy to use, even with complex data • Cons – Can have complex issues with versioning (especially in a strongly typed language like Java) – Sometimes tied to language or library version – Not hand-editable, usually – Be sure you’re only serializing what you intend to – If something is half-written or has concurrency problems, expect to toss the whole file in the trash
XML • Pros – Will parse for you, including some nice searching – XML library ensures you always have a well formatted file – Super portable format that almost anybody can read • Cons – Search/update is still slow – Concurrency and half written files are an issue – Super verbose, and those XML libraries are often a major pain to use
JSON • Pros – Will parse for you – JSON library ensures you always have a well formatted file – Portable format that almost anybody can read (but you usually will need to download a small library for most languages) – Easy to use! Hand editable • Cons – Fancy-smancy corperate folks will complain that you didn’t use XML – Have to read everything into memory so it’s not suitable for giant chunks of data – Concurrency and half written files are an issue
Databases • Pros – Lots of built in protection to prevent corruption and allow concurrency – Fast even with a large amount of data – Has a query language (usually SQL) that lets you do arbitrary kinds of searches on data • Cons – Tricky to set up – Abandon all hopes of hand editing, but you can update with queries – For high performance, you have to understand how things work under the hood
JSON Sample • Please snarf the code for today’s class • You’ll need to download the JSON library Gson (URL is in the snarfed code) • Modify the code to output a Map to a file and read it back • When you’re finished, submit the code via ambient
- Slides: 12