Details of the API for dmc-autostore.

AutoStore Manager API

Properties

data

This is the handle to the "magic" data structure. Start interacting with the data structure using this reference.

main.lua

local AutoStore = require 'dmc_autostore'
local data = AutoStore.data

is_new_file

This flag is primarily intended to give the developer an opportunity to initialize the structure on first app launch.

This is set to true if the data file doesn't exist and false if it does exist.

main.lua

local AutoStore = require 'dmc_autostore'

if AutoStore.is_new_file then
    -- initialize AutoStore data structure
    ...
end

AutoStore Configuration File

The module uses the standard dmc_corona.cfg to read in configuration properties. AutoStore will use its default properties if it's not defined in the config file.

The following is a list of the properties which can be configured:

Properties

TIMER_MIN

This is the minimum amount of time (in milliseconds) which a modification can be saved.

Default is 1000 milliseconds.

dmc_corona.cfg

TIMER_MIN:INT = 1000

TIMER_MAX

This is the maximum amount of time (in milliseconds) which will pass before any modifications are saved.

Default is 4000 milliseconds.

dmc_corona.cfg

TIMER_MAX:INT = 4000

DATA_FILENAME

This is the name of the AutoStore data file. The suffix .json will automatically be appended to the filename.

Default is dmc_autostore.

dmc_corona.cfg

DATA_FILENAME = gamedata

Data Structure API

Methods

Because of the way Lua is architected, the ways to interact with table data structures are slightly different. It actually is more of an object-oriented syntax.

Here is a list of those methods for interacting with the data structure. These can be invoked on table-type nodes (eg, array or hash).

item:pairs()

[Hash] Iterate over the keys in a table. This is similar to pairs(tbl).

Example

main.lua

local AutoStore = require 'dmc_autostore'
local data = AutoStore.data

data.person = { name="John Doe", age=34, eyes="blue" }

local person = data.person
for k,v in person:pairs() do print(k,v) end

output

eyes    blue
name    John Doe
age    34

item:ipairs()

[Array] Iterate over the indicies in a table. This is similar to ipairs(tbl).

Example

main.lua

local AutoStore = require 'dmc_autostore'
local data = AutoStore.data

data.list = { "bread", "butter", "jam" }

local list = data.list
for i,v in list:ipairs() do print(i,v) end

output

1    bread
2    butter
3    jam

item:len()

[Array] Find the size of a table. This is similar to #tbl.

Example

main.lua

local AutoStore = require 'dmc_autostore'
local data = AutoStore.data

data.list = { "bread", "butter", "jam" }

local list = data.list
print( "list length = " .. list:len() )

output

list length = 3

item:insert( val [, pos ] )

[Array] Insert val at position pos. This is similar to table.insert(tbl, val, pos).

Parameter Notes
val value to insert to the table
pos (optional) position at which to place value.

Default is to insert at end of table (eg, append)

Example

local AutoStore = require 'dmc_autostore'
local data = AutoStore.data

data.list = { "bread", "butter", "jam" }

local list = data.list

list:insert( "honey" )
for k,v in list:pairs() do print(k,v) end

print( "" )

list:insert( "pasta", 2 )
for k,v in list:pairs() do print(k,v) end

output

1    bread
2    butter
3    jam
4    honey

1    bread
2    pasta
3    butter
4    jam
5    honey

item:remove( [pos] )

[Array] Remove element at position pos. This is similar to table.remove(tbl, pos).

Returns element removed.

Parameter Notes
pos (optional) position from which to remove value.

Default is to remove last element in the table.

Example

local AutoStore = require 'dmc_autostore'
local data = AutoStore.data

data.list = { "bread", "butter", "jam", "honey" }

local list = data.list

list:remove() -- remove 'honey'
for k,v in list:pairs() do print(k,v) end

print( "" )

list:remove( 1 ) -- remove 'bread'
for k,v in list:pairs() do print(k,v) end

output

1    bread
2    butter
3    jam

1    butter
2    jam