The begin
First of all, we need to declare the table. If you want data to be stored in a single level, then use the Level.filename() function for the table, this works only for luna.lua files. Then, we create a new variable to store the table, so we can use it later:
Code: Select all
SaveData[Level.filename()] = SaveData[Level.filename()] or {}
local levelsave = SaveData[Level.filename()]
Code: Select all
SaveData["episode"] = SaveData["episode"] or {}
local globalsave = SaveData["episode"]
Starting to use SaveData
To set a new value, we just write its name and set it a value, it can be a number, string, boolean or even another table:
Code: Select all
levelsave.n = 30
Code: Select all
function onStart()
if levelsave.n == nil then
levelsave.n = 0
end
end
That's how typically I initialize values because of how I use them, but always initialize them at onStart() or before any of the lua events.
How can we use level data outside levels?
If we want to access a level's data from the worldmap (map.lua), we need to get the level object by using the Level instance functions (only within a loop). In the following example, we want the data from the level we stand on, so we check if we sat foot on some level and we print a value from it:
Code: Select all
function onTick()
if world.levelObj ~= nil then
local lev = world.levelObj
for i,k in ipairs(Level.get()) do
if k == lev then
SaveData[k.filename] = SaveData[k.filename] or {}
local levelsave = SaveData[k.filename]
Text.print(tostring(levelsave.n), 3, 400, 400)
end
end
end
end
What is GameData? How to use it?
GameData is a table like SaveData, but with a different behavior. SaveData values are stores permanently in an episode, but GameData values are erased when the game closes. The usage of GameData follows exactly the same logic of SaveData:
Code: Select all
GameData[Level.filename()] = GameData[Level.filename()] or {}
local gamesave = GameData[Level.filename()]
____________________
Comment below if you have any doubts or questions. See you later.