EpicPrefs by Code

In this article we will learn how to make use of EpicPrefs by Code.

The usage of EpicPrefs is very simple.

PlayerPrefs allow the following:

  • PlayerPrefs.GetString(string key, (optional)string defaultValue); and PlayerPrefs.SetString(string key, string value);
  • PlayerPrefs.GetInt(string key, (optional)int defaultValue); and PlayerPrefs.SetInt(string key, int value);
  • PlayerPrefs.GetFloat(string key, (optional)float defaultValue); and PlayerPrefs.SetFloat(string key, float value);

In order to be a direct replacement for the PlayerPrefs, EpicPrefs implements a wrapper around its actual getters and setters that mimic the PlayerPrefs accessors:

  • EpicPrefs.GetString(string key, (optional)string defaultValue); and EpicPrefs.SetString(string key, string value);
  • EpicPrefs.GetInt(string key, (optional)int defaultValue); and EpicPrefs.SetInt(string key, int value);
  • EpicPrefs.GetFloat(string key, (optional)float defaultValue); and EpicPrefs.SetFloat(string key, float value);

However, EpicPrefs offers a much more flexible and general way of getting and setting Prefs, namely:

  • EpicPrefs.Set<T>(string name, T value, bool encrypted = false)
  • EpicPrefs.Get<T>(string name, bool encrypted = false)
  • EpicPrefs.Get<T>(string name, T defaultValue = default, bool encrypted = false)

The complete list of supported types would probably make this documentation explode, but as a general rule of thumb everything that does not have a reference to an instance in the scene can be saved and retrieved this way. The Set method allows you to pass in a Name, value and a bool specifying if the Pref should be AES encrypted or not. It will return true if successfully saved.

  • Name can be any string. If you use the same name twice for different Types they will not collide, if you use them for the same Type it will overwrite any previous EpicPref.
  • The value can be of any type ( see above ) and the type does not even have to be specified.
  • If true is passed, the Pref will be Encrypted with the safety layer chosen when creating the encryption keys. If false is passed, or the third parameter is omitted, they will not be encrypted but nonetheless the names will be hashed and the values will be serilized to a binary format.

The Get method allows you to pass in a Name, default value and a bool specifying if the Pref was AES encrypted or not.

  • The Name has to match the name previously used to Set the Pref
  • The (optional) default value will be returned if the requested Pref does not exist. If omitted, the Type T has to be specified when getting a Pref like this : EpicPrefs.Get<string>("Name"). If no default value is passed and a pref does not exist, the default value for the given type will be returned.
  • If true is passed, the returned pref was encrypted while setting. If false or omitted, the pref wasn't encrypted when saving.

If a pref was chosen to be encrypted upon saving, it has to be retrieved as encrypted aswell. The same name can be used without collision for encrpyted and non-encrypted values, e.g. :

EpicPrefs.Set("TestA","ABC",true);
EpicPrefs.Set("TestB","DEF",false);
var testA = EpicPrefs.Get("TestA",true);
var testB = EpicPrefs.Get("TestB",false);

will result in testA==="ABC" and testB==="DEF".

Next up is the Visual Editor