LevelData.lua
LevelData is a small library which wraps around the UserData functions, making level-specific variables easier to manage.
Installation
Drop the LevelData.lua file in the LuaScriptsLib folder.
How to use
First, load in the API. Then you can use LevelData functions as you would use UserData functions, to save, load, and manage custom saved variables.
Example
LevelData = loadAPI("LevelData");
function onLoad()
if(LevelData.isValueSet("Var")) then
LevelData.setValue("Var", 0);
LevelData.save();
end
end
function onLoop()
local i = LevelData.getValue("Var");
i = i + 1;
LevelData.setValue("Var", i);
LevelData.save();
end
This will increment a value saved in the UserData variables, but crucially, the same value can be used in multiple levels, and will not clash with each other, allowing each level to keep track of their own variable. If used on the overworld, this library will also create a variable for the overworld.
Documentation
External Use Functions
These are functions you will need to use the library
| getValue | Key | ||
|---|---|---|---|
| Gets a value saved to this level's data bank. | number or nil | string
The key used to save the value to the data bank. | |
| setValue | Key | Value | |
| Sets a value in this level's data bank to the specified value. | nil | string
The key to save the value to the data bank. |
number
The value to assign to this key. |
| isValueSet | Key | ||
| Determines whether or not a value has been saved to this level's data bank. | boolean | string
The key used to save the value to the data bank. | |
| values | |||
| Returns a list of all keys in this level's data bank. | table(string,number) | ||
| save | |||
| Flushes the data to the external text file, ensuring that it can be read back later. | nil | ||
| externalData | levelName | ||
| Creates a new LevelData object, which allows access to a level defined by the levelName variable. For example, if levelName is given as "Level", this object will access variables saved for Level.lvl. To access data for the overworld, this should be set to ".MAP". | LevelData | string
The name of the filename for the level whose data you wish to access (do not include ".lvl" on the end of this). |