playerprefs Documentation


playerprefs_get

Syntax:

playerprefs_get(key, [default ]);
Argument name Type Description
key string The key to find the playerpref saved value
default string|real|undefined The default value to return if the key does not exists

Returns: (string|real|undefined) The saved playerpref value

Description:

Gets the value of the preference identified by key. If it doesn't exist, it will return the default value.

Example:

var name = playerprefs_get("player_name");
var health = playerprefs_get("health", 0);

playerprefs_has

Syntax:

playerprefs_has(key);
Argument name Type Description
key string The key to check the playerpref saved value

Returns: (boolean) True or false depending if there is a saved playerpref with that key

Description:

Returns true if key exists in the preferences.

Example:

var first_play = playerprefs_has("first_play");
if (!first_play) {
show_message_async("Welcome to my super game!");
}

playerprefs_delete

Syntax:

playerprefs_delete(key);
Argument name Type Description
key string The key to remove the playerpref saved value

Returns: (undefined) Undefined

Description:

Removes key and its corresponding value from the preferences and returns undefined.

Example:

if (show_question("Want to remove all the progress in this level?")) {
level_12_score = playerprefs_delete("level_12_score");
show_message("Progress of the level 12 erased");
}

playerprefs_delete_all

Syntax:

playerprefs_delete_all();

Returns: (undefined) Undefined

Description:

Removes all keys and values from the preferences. Use with caution.

Example:

 if (show_question("Want to reset all the progress in the game?")) {
playerprefs_delete_all();
show_message("Progress erased");
}

playerprefs_set

Syntax:

playerprefs_set(key, value);
Argument name Type Description
key string The key of the playerpref saved value
value string|real The value to save

Description:

playerprefs_set(key, value);


playerprefs_save

Syntax:

playerprefs_save();

Description:

Writes all modified preferences to disk. Normally, there is no need to call this function, since playerprefs_gm will automatically call it for you.

You can use it to force the saving in critical points of the game/application.

Note: There is no need to call this function manually inside game end event.

Note: On mobile platforms preferences are saved during application suspend, because there's no game end event.


playerprefs_system_init

Syntax:

playerprefs_system_init();

Description:

Call this script at the create event of the obj_playerprefs


playerprefs_get_instance

Syntax:

playerprefs_get_instance();

Returns: (instance) The obj_playerpref instance

Description:

Normally you DON'T NEED to use this funcion. This is an advanced function. But if you need for some reason access to the obj_playerprefs DO NOT ACCESS IT manually, use playerprefs_get_instance() to get a reference to the obj_playerprefs.


singleton

Syntax:

singleton();

Returns: (boolean) See example

Description:

Use this script to create a singleton persistent instance. You can find more information about singleton in google. But basically, the singleton model, let you have a single instance of an object that is persistent an can be used to control the game. (The singleton model is a lot more complex, but I adapted it to be compatible to game maker studio)

USAGE: 1- call the script at the FIRST LINE OF CODE of the creation event of your obj_control or the instance you want to be a singleton. In this way:

if (singleton()) {exit;}

2- Place this object in the first room of your game. Now, you don't need to worry for the number of obj_control you have. This script will permits ONLY ONE INSTANCE at time of this object.

Note: This script is used by the Playerprefs Engine. Please do not delete it.


playerprefs_system_step

Syntax:

playerprefs_system_step();

Description:

Call this in the step event of the obj_playerprefs