To manage persistent data, like user preferences
or a simple save game values,
use CastleConfig unit
with a UserConfig singleton inside. A simple example:
uses CastleWindow, CastleConfig, CastleApplicationProperties, CastleControls, CastleColors; var Window: TCastleWindow; LabelForParameter: TCastleLabel; MyParameter: string; begin { set ApplicationName, this is used by UserConfig.Load to determine config file location. } ApplicationProperties.ApplicationName := 'my_game_name'; { open Window } Window := TCastleWindow.Create(Application); Window.Container.BackgroundColor := White; Window.Open; { load UserConfig } UserConfig.Load; { load parameter from UserConfig } MyParameter := UserConfig.GetValue('my_parameter', 'default_value'); { create UI and show show parameter from UserConfig } LabelForParameter := TCastleLabel.Create(Application); LabelForParameter.Caption := 'My parameter is now equal: ' + MyParameter; Window.Controls.InsertFront(LabelForParameter); { do the main part of your program } // MyParameter := 'some other value'; // test Application.Run; { save parameter } UserConfig.SetValue('my_parameter', MyParameter); // or like this: UserConfig.SetDeleteValue('my_parameter', MyParameter, 'default_value'); { save UserConfig } UserConfig.Save; end.
To load and save config values, you should use GetValue
and SetValue (or SetDeleteValue) methods.
See the TXMLConfig
class documentation. These provide basic means to load/save
integers, booleans and strings in a simple XML format.
We extend the standard TXMLConfig with more
methods:
BlowFishKeyPhrase property).
  GetValue, GetVector3 etc.)
    that require the presence of given attribute in the XML file.
    They raise an exception when the attribute is missing or invalid.
    This is useful if you want to somewhat validate the XML file
    by the way (for example when it's a game model file that must contain given
    variables).
See the TCastleConfig for a documentation of our extensions.
While you can load and save the config data at any time,
you can also register your own load and save listeners using
the
TCastleConfig.AddLoadListener,
TCastleConfig.AddSaveListener mechanism. This sometimes allows to decentralize your code better.
On Android, our engine allows to easily upload and download the savegames using the Google Play Games "Saved Games" feature. To use this feature:
Turn on the Google Play Games integration for your project.
Create and initialize the
    TGameService    instance in your code. Be sure to pass parameter SaveGames
    as true to the TGameService.Initialize
    call.
  
Connect player to the Google Play Games at runtime,
    using TGameService.RequestSignedIn method,
    and / or passing AutoStartSignInFlow as true
    to the TGameService.Initialize call.
    
You can wait for the sign-in to happen by the
    TGameService.OnSignedInChanged    event, or just observe the
    TGameService.SignedIn    property.
  
Then load and save games using the
    TGameService.SaveGameLoad and
    TGameService.SaveGameSave    methods. They represent the "savegame contents" as a simple string,
    and you can use the UserConfig.SaveToString
    and UserConfig.LoadFromString methods
    to trivially upload / download the UserConfig
    contents to the cloud!
  
If you want to allow user to choose a "slot" where to save the game,
    or from which to load the game, you can use a ready dialog by calling
    TGameService.ShowSaveGames.
castle-config:/ URLsYou're not limited to storing the data using UserConfig.
You can create any files and organize them in any subdirectory hierarchy by saving and loading using castle-config:/ URLs. Follow the URLs, loading (downloading) and saving resources documentation for more information.