Download presentation
Presentation is loading. Please wait.
Published byDerick O’Neal’ Modified over 9 years ago
1
Storing Data
2
A Note About Creating Games
3
Why do you want to store data? 1.Data files 2.Configuration files
4
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
5
Example Config File Application: Internationalized Applications Instead of JButton myButton = new JButton(“Press Here to Continue”); JButton myButton = new JButton(StringConfig.getStringFor(“PRESS_HERE”));
6
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.Serializing 3.XML 4.JSON 5.Databases
7
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
8
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
9
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
10
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
11
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
12
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.