Recently, a lot of csl users have been asking questions about the saving/loading features. So, we have decided to do an official writeup on the topic.
To get started, load the library into your code. (You can find it here)
require "crawlspaceLib"
For this demo, we are going to keep track of “points” using the data features of the crawl space library. Csl has a fuction called Defaults that sets everything up for us. We just need to pass in a table.
Defaults ( {
points = 0
} )
On the first run, that code will set up everything we need to use saving and loading functionality. Now we need to load any existing data.
Load()
After that, we can use setVar() and getVar() to read and update the default data.
setVar( {'points', 100} )
Save()
That will increment “points” by 100 every time it is run. You can verify this by printing out the variable.
print(getVar('points'))
That is all there is to it. The crawl space library makes saving and loading data a breeze. Here is the code all together:
require "crawlspaceLib"
Defaults ( {
points = 0
} )
Load()
setVar( {'points', 100} )
Save()
print(getVar('points'))
Let’s look at a more practical example. Here is a high score system made with csl:
require "crawlspaceLib"
Defaults ( {
points = 0,
highScore = 0
} )
Load()
local gameStart = function()
setVar( {‘points’, 0, true} )
end
local addPoints = function(points)
setVar( {‘points’, points} )
end
local gameOver = function()
local score = getVar(‘points’)
if score > getVar(‘highScore’) then
setVar( {‘highScore’, score, true} )
end
end
-- sample gameplay
gameStart()
for i=1, math.random(1,10) do
addPoints(100)
end
gameOver()
–
print("Points: "..getVar('points'))
print(“High Score: “..getVar(‘highScore’))
Save()
Breaking this into pieces, first we set up the defaults and load existing data.
require "crawlspaceLib"
Defaults ( {
points = 0,
highScore = 0
} )
Load()
Next we make a few functions to help us out:
We are going to track our current points with csl. addPoints() simply uses setVar() to add to the “points” value.
local addPoints = function(points)
setVar( {‘points’, points} )
end
We also need to be able to reset the points at the start of each game. The gameStart() function uses csl to set “points” back to 0. Now normally, setVar() will increment itself if it can. In this case, we need to pass true as a third parameter. This parameter tells setVar() to set the variable instead of adding to it.
local gameStart = function()
setVar( {‘points’, 0, true} )
end
To wrap up the game, we should check if the current “points” are greater than “highScore”. If they are, we need to update with the new high score.
local gameOver = function()
local score = getVar(‘points’)
if score > getVar(‘highScore’) then
setVar( {‘highScore’, score, true} )
end
end
Next we have a simple loop that demonstrates what a game might look like:
gameStart()
for i=1, math.random(1,10) do
addPoints(100)
end
gameOver()
First we reset “points”. Next we randomly add to “points”. Finally, If there is new high score, we set it.
Now we can wrap it all up by saving our data and printing it out.
print("Points: "..getVar('points'))
print(“High Score: “..getVar(‘highScore’))
Save()
Hopefully this gives a pretty clear picture of the power and ease of the data functions in the Crawl Space Library. This is just scratching the surface of what csl can be used for. There are even more features in the Save/Load functions, such as saving any table and saving to a non-default file:
local levels = {1,2,3,4}
local filename = “levels.txt”
Save(levels,filename)
There you have it, an easy way to store data using the Crawl Space Library. There are tons of uses for these features, and I am looking forward to seeing what you come up with. Go checkout csl if you haven’t already. It has a lot of cool stuff and more just around the corner. Again, you can find that here. Feel free to drop us a comment below and share what you have been using the library for.
Enjoy! –Lance