Game Maker Web Local/Session Storage & Cookies Extension

This extension allows you to write, read or remove data to/from the browsers local storage, session storage and cookies.

All modern browsers support Local and Session Storage. However, it is better to check if a player’s browser supports that. Use web_local_storage_is_supported

Check First

web_local_storage_is_supported()
Description
: Returns whether the browser supports Local & Session Storage. All modern browsers do that, no worries! But, just in case…
Returns : true or false (Boolean)

Local Storage

Local storage is the storage unit that can store data even after the player closes the browser. If a player gets 1000 coin in your game, and closes the game, you can give that 1000 coin back once the player reopens your game.

The data here is saved into and stored in the user’s browser. User can remove this data from the browser menu.

web_local_storage_set(String Key, String Value);
Description
: Adds/Sets a Key <> Value to the Local Storage.
Returns : N/A

web_local_storage_set("Nickname","Marty");

web_local_storage_get(String Key);
Description
: Returns the value of a key. Remember that it always returns as String.
Returns : Value (String)

var nickname = web_local_storage_get("Nickname");
draw_text(20,20,"His nickname is : "+string(nickname));

web_local_storage_remove(Key);
Description
: Removes a key from the Local Storage.
Returns : N/A

web_local_storage_clear();
Description
: Clears all the keys from the Local Storage.
Returns : N/A

Session Storage

Session Storage is the storage unit that is automatically removed once the player closes THE TAB. Even each tab has their own session storage, so the data is really limited to a certain session. You should use this to hold temporary data, the data you would not need to hold for more than one gameplay.

The data stored here is held in user’s browser. It will be completely wiped out once the user closes the tab, or the browser.

web_session_storage_set(String Key, String Value);
Description
: Adds/Sets a Key <> Value to the Session Storage.
Returns : N/A

web_session_storage_get(String Key);
Description
: Returns the value of a key. Remember that it always returns as String.
Returns : Value (String)

web_session_storage_remove(String Key);
Description : Removes a key from the Session Storage.
Returns : N/A

web_session_storage_clear();
Description : Clears everything from the Session Storage. Remember that closing the game/tab/browser removes everything too.
Returns : N/A

Cookies

The cookies, we all know and love; right 🙂 So, cookies are a bit different than local storage, that server can also access to them as well. Although in a Game Maker game, we wouldn’t need them much; there’s nothing stopping us not to using them.

web_cookie_set(String Key, String Value, Real ExpirationSeconds);
Description
: Adds/Sets a Key <> Value cookie. This cookie will be removed automatically after ExpirationSeconds(in seconds). Value must always be a STRING.
Returns : N/A

web_cookie_set("LoggedIn","TRUE", 3600); 
//In 1 hour(3600 seconds), this will be removed

web_cookie_get(Key);
Description
: Gets a Key’s value. If the key doesn’t exists, it returns “N/A”
Returns : Value (String) or N/A (String)

var isLoggedIn = web_cookie_get("LoggedIn");
if (isLoggedIn=="TRUE")
{
show_debug_message("Yea the user has logged in before");
}
else
{
show_debug_message("No, the user hasn't logged in");
}

web_cookie_remove(Key);
Description
: Simply expires a cookie, removes them from the system.
Returns : N/A

///User logs out...
web_cookie_remove("LoggedIn");
GET THE EXTENSION

Open Source

Share

Marty

Truely speaking, I don't know what am I doing most of the time.

Leave a Reply