Difference between revisions of "Bot Playground/Built-in Functions/localStorage.set"

From SmartBots Developers Docs
Jump to: navigation, search
(Created page with "{{DISPLAYTITLE:{{SUBPAGENAME}}}} <onlyinclude>Puts a string value into a persistent storage.</onlyinclude> Also check localStorage.get(). <syntaxhighligh...")
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{DISPLAYTITLE:{{SUBPAGENAME}}}}
 
{{DISPLAYTITLE:{{SUBPAGENAME}}}}
<onlyinclude>Puts a string value into a persistent storage.</onlyinclude> Also check [[localStorage.get|localStorage.get()]].
+
<onlyinclude>Puts a string value into a persistent storage.</onlyinclude> Also check [[Bot_Playground/Built-in_Functions/localStorage.get|localStorage.get()]].
  
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
localStorage.set("old_data", "A value to store");
+
localStorage.set(name, value);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 10: Line 10:
  
 
{{API Variable Group|Input}}
 
{{API Variable Group|Input}}
{{API Variable|name|yes}} the name of the persistent value you would like to get.
+
{{API Variable|name|yes}} the name of the persistent value you would like to store
JSON.stringify()
+
{{API Variable|value|yes}} the value you want to save.
 
{{API Variable Group|Output}}
 
{{API Variable Group|Output}}
 
{{API Return none}}
 
{{API Return none}}
  
 
{{API Variables Table End}}
 
{{API Variables Table End}}
 +
 +
== Limitations ==
 +
 +
localStorage is intended to store short key-value pairs. The size of the value is limited to 6144 bytes. If you exceed this size script generates a warning:
 +
 +
  localStorage value is more than 6144 bytes and not persistent
 +
 +
If you store significant amount of data (like dozen of keys, few kilobytes each), consider using your own key-value database using http requests.
  
 
== Comments ==
 
== Comments ==
  
This function works similar to browser JavaScript localStorage.get(). It retrieves value from a persistent storage.
+
This function works similar to browser JavaScript localStorage.set(). It saves value into a persistent storage.
  
 
localStorage is shared by all scripts of the same bot. Thus, you can set values in one bot script and retrieve it in another script.
 
localStorage is shared by all scripts of the same bot. Thus, you can set values in one bot script and retrieve it in another script.
 +
 +
Also note that localStorage supports string values only. To save something more complicated, use JSON.stringify(var).
 +
 +
== Inter-script communication ==
 +
 +
Executing localStorage.set() will cause [[Bot Playground/Built-in Functions/localStorage.on|localStorage.on("update")]] event to fire on ''other scripts of this bot''.
  
 
{{NavMenu}}
 
{{NavMenu}}

Latest revision as of 10:38, 16 April 2025

Puts a string value into a persistent storage. Also check localStorage.get().

localStorage.set(name, value);

Reference

This command accepts the following parameters:

Variable Required Description


Input:
name yes the name of the persistent value you would like to store
value yes the value you want to save.
Output:
result This function does not return anything

Limitations

localStorage is intended to store short key-value pairs. The size of the value is limited to 6144 bytes. If you exceed this size script generates a warning:

 localStorage value is more than 6144 bytes and not persistent 

If you store significant amount of data (like dozen of keys, few kilobytes each), consider using your own key-value database using http requests.

Comments

This function works similar to browser JavaScript localStorage.set(). It saves value into a persistent storage.

localStorage is shared by all scripts of the same bot. Thus, you can set values in one bot script and retrieve it in another script.

Also note that localStorage supports string values only. To save something more complicated, use JSON.stringify(var).

Inter-script communication

Executing localStorage.set() will cause localStorage.on("update") event to fire on other scripts of this bot.