12
Storing Data

Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

Embed Size (px)

Citation preview

Page 1: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

Storing Data

Page 2: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

A Note About Creating Games

Page 3: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

Why do you want to store data?

1. Data files2. Configuration files

Page 4: 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 stuffCons:• You now must consider file management

Page 5: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

Example Config File Application: Internationalized Applications

• Instead of

JButton myButton = new JButton(“Press Here to Continue”);

JButton myButton = new JButton(StringConfig.getStringFor(“PRESS_HERE”));

Page 6: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

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. Write the file format yourself (flat file)2. Serializing3. XML4. JSON5. Databases

Page 7: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

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

Page 8: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

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

Page 9: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

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

Page 10: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

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

Page 11: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

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

Page 12: Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files

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