1 / 12

Storing Data

Storing Data. A Note About Creating Games . Why do you want to store data?. Data files 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

tana
Télécharger la présentation

Storing Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Storing Data

  2. A Note About Creating Games

  3. Why do you want to store data? • Data files • 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 JButtonmyButton = new JButton(“Press Here to Continue”); JButtonmyButton = new JButton(StringConfig.getStringFor(“PRESS_HERE”));

  6. How do I store data? • The right answer is almost always not write a file format yourself • Know and use the right tool for the job: • Write the file format yourself (flat file) • Serializing • XML • JSON • 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-smancycorperate 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

More Related