FlashMemory

FlashMemory

The FlashMemory module allows you to store persistent data while your gadget is powered off or even put away while you're not playing Retro Gadgets.

Sizes

FlashMemory modules have a capacity for how much data they can store, and each different chip size has a different capacity:

small FlashMemory The small FlashMemory can store 4096 bytes.

medium FlashMemory The medium FlashMemory can store 32768 bytes.

large FlashMemory The large FlashMemory can store 256000 bytes.

Properties

Size number read only

Returns the overall capacity of the memory in bytes.

Usage number read only

Returns the amount of data in bytes currently stored.

Methods

Save( table table ) boolean

Saves table into the flash memory. Returns true if saving was successful, false otherwise.

Load( ) table

Returns the table stored in the memory.

Examples

To store data in a FlashMemory, your data must be in the form of a table.

If you want to store a default number value of 100, for example:

-- first we check if the flash is empty, meaning there is no data in it yet
-- this way we can store something default only if there isn't already saved data
local value = 100

if gdt.FlashMemory0.Usage == 0 then
	-- we put the value we want to store into a table
	-- constructing the table like so gives it a "value" key
	local data = {
		value = value
	}

	gdt.FlashMemory0:Save(data)
end

Once the gadget starts, it will check if there is already data saved in the FlashMemory. If there isn't it'll create a data table with a key value containing our value of 100, and save it.

We can then retrieve the same table that is in the FlashMemory again:

local data = gdt.FlashMemory0:Load()

-- this assumes your data table was given a "value" key
-- otherwise you can index it like any other table by numerical index: data[1]
log(tostring(data.value))