Difference between revisions of "Saving Local Settings"

From Slick2D Wiki
Jump to: navigation, search
(Created page with "Local settings are easy to save when you're working with a local file system and no security settings. However, if you're deploying via webstart you may find your game in a sa...")
 
Line 20: Line 20:
  
 
Remember, the two storage systems (file based and muffin based) are not compatible with each other and hence if you test using file based storage your settings will not be available when you use your webstart deployment.
 
Remember, the two storage systems (file based and muffin based) are not compatible with each other and hence if you test using file based storage your settings will not be available when you use your webstart deployment.
 +
 +
[[Category:Miscellaneous]]

Revision as of 15:46, 8 August 2013

Local settings are easy to save when you're working with a local file system and no security settings. However, if you're deploying via webstart you may find your game in a sandboxed environment that prevents direct file access - so how are we going to save game settings?

Webstart provides a service referred to as Muffins (like HTTP Cookies, but for webstart). However the interface to these is fairly complex. Additionally, it'd be useful if when developing you could swap and change between a file based storage and a muffin based storage at deployment.

Slick provides exactly this. The SavedState class provides methods to load and save arbitrary game properties to an abstract storage area. Underneath this storage will switch between file based or muffin based dependent on the operating environment. Note that this storage will not currently work within Applets - this is still an open issue.

The saved state class provides methods to store numbers and string seperately. To save data simply set the data using:

public void setNumber(String nameOfField, double value)
public void setString(String nameOfField, String value)

and finally commit your data by calling save(). When loading data first get the data from the underlying implementation by calling load(). Then use the following methods to extract the previously stored data:

public double getNumber(String nameOfField)
public double getNumber(String nameOfField, double defaultValue)
public String getString(String nameOfField)
public String getString(String nameOfField, String defaultValue)

Note that when loading data you can specify default values. If the field name provided does not exist in the saved data then the default value is returned.

Remember, the two storage systems (file based and muffin based) are not compatible with each other and hence if you test using file based storage your settings will not be available when you use your webstart deployment.