|  Developer
Updated on November 14, 2021

Storage module API (require “storage”)

, , ,

  • Get <value from storage with corresponding key.
Edit

  • params: key – string
  • return: value if it exists and can be converted to specified type, otherwise
Edit
				
					require "storage"
local val = storage.get_number("Pi")				
			

  • Put bool value to storage with corresponding key
Edit

  • params: key – string, value – bool
  • return: none
Edit
				
					require "storage"
storage.set_bool("IsNodeAdded", true)
print(storage.get_bool("IsNodeAdded")) -- output: true				
			

  • Put string value to storage with corresponding key
Edit

  • params: key – string, value – string
  • return: none
Edit
				
					require "storage"
storage.set_string("MainItemName", "zyz")
print(storage.get_string("MainItemName")) -- output: zyz				
			

  • Put number value to storage with corresponding key
Edit

  • params: key – string, value – number
  • Note, lua has ‘number’ type but it can can be in integer state. The difference between states are in conversion to string and printing. saves value in integer state if possible. See example for details.
  • return: none
Edit
				
					require "storage"
storage.set_number("ItemsCount", 50)
print(storage.get_number("ItemsCount")) -- output: 50

storage.set_number("Pi", 3.14159)
print(storage.get_number("Pi")) -- output: 3.14159

storage.set_number("AverageTemperature", 20.0)
print(storage.get_number("AverageTemperature")) -- output: 20 (not 20.0)				
			

  • Put table value to storage with corresponding key
Edit

  • params: key – string, value – table
  • Supported table key types: string, number, bool Supported table value types: string, number, bool, table. Number values are saved as integer if possible (see for details).
  • Note, recurse tables and metatables are not supported.
  • return: none
Edit
				
					require "storage"
storage.set_table("ItemData", {
        name = "xyz",
        type = "switch"
    })

storage.set_table("SomeData", {
        name = "xyz",
        [1] = true,
        [3] = false,
        [true] = {
            [1.1] = 7
        }
    })

local result = storage.get_table("SomeData")
print(result.name)       -- output: xyz
print(result[1])         -- output: true
print(result[true][1.1]) -- output: 7 				
			

  • Check if key is set.
Edit

  • params: key – string
  • return: if key is set, otherwise
Edit
				
					require "storage"
if storage.exists("Pi") then
    print("Pi is stored")
else
    print("Pi is not stored")
end				
			

  • Delete value from storage marked with key. Function has no effect if key does not exist.
Edit

  • params: key – string
  • return: none
Edit
				
					require "storage"

storage.delete("Pi")				
			

  • Clear storage. Deletes all key/value pairs from current volume
Edit

  • params: none
  • return: none
Edit
				
					require "storage"
storage.delete_all()