Angel 3.2
A 2D Game Prototyping Engine
Public Member Functions | Static Public Member Functions | List of all members
Preferences Class Reference

A centralized class that handles any persistent preferences. More...

#include <Preferences.h>

Public Member Functions

void SavePreferences ()
 
int GetInt (const String &category, const String &name)
 
float GetFloat (const String &category, const String &name)
 
String GetString (const String &category, const String &name)
 
LoadedVariableMap GetTable (const String &category)
 
void SetInt (const String &category, const String &name, int val)
 
void SetFloat (const String &category, const String &name, float val)
 
void SetString (const String &category, const String &name, const String &val)
 
int OverrideInt (const String &category, const String &name, int val)
 
float OverrideFloat (const String &category, const String &name, float val)
 
String OverrideString (const String &category, const String &name, String val)
 
const String GetDefaultPath ()
 
const String GetUserPrefsPath ()
 

Static Public Member Functions

static PreferencesGetInstance ()
 

Detailed Description

We very often want to define persistent preferences for the user. They are stored out in .lua files, so they can be easily written or inspected by hand if needed.

When the game starts, the game will first load preferences from Config/defaults.lua, then load saved preferences, overriding any default preferences.

Preferences are organized into categories, represented as discrete tables in the .lua files. For example, if this was the preferences file:

GameStart = {
enemies = 15,
heroName = "Superdude",
}

Then we would retrieve the preference using "GameStart" as the category, and "enemies" or "heroName" as the value. Preferences can either be integers, floats, or Strings.

Definition at line 60 of file Preferences.h.

Member Function Documentation

Preferences & Preferences::GetInstance ( )
static

Used to access the singleton instance of this class. As a shortcut, you can just use "thePrefs".

Returns
The singleton

Definition at line 42 of file Preferences.cpp.

void Preferences::SavePreferences ( )

Writes out all currently set preferences to a user-writable directory, which will override the defaults the next time the game launches.

Definition at line 56 of file Preferences.cpp.

int Preferences::GetInt ( const String &  category,
const String &  name 
)

Retrieves a given preference in integer form.

Parameters
categoryThe category of the desired preference
nameThe value of the desired preference
Returns
The retrieved integer (0 if this preference has never been set)

Definition at line 93 of file Preferences.cpp.

float Preferences::GetFloat ( const String &  category,
const String &  name 
)

Retrieves a given preference in float form.

Parameters
categoryThe category of the desired preference
nameThe value of the desired preference
Returns
The retrieved float (0.0f if this preference has never been set)

Definition at line 109 of file Preferences.cpp.

String Preferences::GetString ( const String &  category,
const String &  name 
)

Retrieves a given preference in String form.

Parameters
categoryThe category of the desired preference
nameThe value of the desired preference
Returns
The retrieved String (Empty string if this preference has never been set)

Definition at line 125 of file Preferences.cpp.

LoadedVariableMap Preferences::GetTable ( const String &  category)

Gets all variables in a given category.

Parameters
categoryThe desired category
Returns
All defined preferences for this category

Definition at line 141 of file Preferences.cpp.

void Preferences::SetInt ( const String &  category,
const String &  name,
int  val 
)

Sets a preference as an integer. If it did not previously exist, it will be created.

Parameters
categoryThe category of the preference to set
nameThe value of the preference to set
valThe actual value we want to store

Definition at line 161 of file Preferences.cpp.

void Preferences::SetFloat ( const String &  category,
const String &  name,
float  val 
)

Sets a preference as a float. If it did not previously exist, it will be created.

Parameters
categoryThe category of the preference to set
nameThe value of the preference to set
valThe actual value we want to store

Definition at line 175 of file Preferences.cpp.

void Preferences::SetString ( const String &  category,
const String &  name,
const String &  val 
)

Sets a preference as a String. If it did not previously exist, it will be created.

Parameters
categoryThe category of the preference to set
nameThe value of the preference to set
valThe actual value we want to store

Definition at line 189 of file Preferences.cpp.

int Preferences::OverrideInt ( const String &  category,
const String &  name,
int  val 
)

Takes a given integer, and returns either that integer or, if an appropriate preference exists, will return it. Handy for allowing hardcoded values to be overridden by user-defined ones.

Parameters
categoryThe category of the preference to check
nameThe value of the preference to check
valThe value to return if there is no such preference
Returns
Either the given integer or the appropriate preference integer

Definition at line 203 of file Preferences.cpp.

float Preferences::OverrideFloat ( const String &  category,
const String &  name,
float  val 
)

Takes a given float, and returns either that float or, if an appropriate preference exists, will return it. Handy for allowing hardcoded values to be overridden by user-defined ones.

Parameters
categoryThe category of the preference to check
nameThe value of the preference to check
valThe value to return if there is no such preference
Returns
Either the given float or the appropriate preference float

Definition at line 219 of file Preferences.cpp.

String Preferences::OverrideString ( const String &  category,
const String &  name,
String  val 
)

Takes a given String, and returns either that String or, if an appropriate preference exists, will return it. Handy for allowing hardcoded values to be overridden by user-defined ones.

Parameters
categoryThe category of the preference to check
nameThe value of the preference to check
valThe value to return if there is no such preference
Returns
Either the given String or the appropriate preference String

Definition at line 235 of file Preferences.cpp.

const String Preferences::GetDefaultPath ( )

Used by the scripting system when it attempts to load the preferences files at game start.

Returns
The absolute path to the Config/defaults.lua file

Definition at line 82 of file Preferences.cpp.

const String Preferences::GetUserPrefsPath ( )

Used by the scripting system when it attempts to load the preferences files at game start.

Returns
The absolute path to the file which stores any preferences that were saved with thePrefs.SavePreferences()

Definition at line 87 of file Preferences.cpp.


The documentation for this class was generated from the following files: