Data files
Game variables can be saved in simple text files. This is helpful for saving a game state, high score table etc.
-- Example of variables
local swell = 5
local wave = 10
local location = "St Ives"
local tide = "high"
-- Put the variables in a table
local save_data = {
swell_saved = swell,
wave_saved = wave,
location_saved = location,
tide_saved = tide
}
-- Save the table to the local user path
local file_path = sys.get_save_file("folder","filename")
sys.save(file_path, save_data)
-- Load the data
local load_data = sys.load(file_path)
local swell = load_data["swell_saved"]
local wave = load_data["wave_saved"]
local location = load_data["location_saved"]
local tide = load_data["tide_saved"]
The local variables swell, wave, location and tide are put into a table called save_data. The table is saved to the local user file path. This is often C:\Users\user\AppData\Roaming\folder\filename
The table is read back in from the file into the variable load_data. Individual data items in the table can be accessed by the name of the saved variable.
If the data is already stored in a table there is no need to create a new table, simply save the exisiting table. JSON files can also be used as a structured data store using sys.load_resource()
Remember that browsers do not allow access to the file system for security, so HTML games will require the use of cookies to store data instead.