yegorf1 wrote:
I wanted do with love.filesystem
Maybe this can help you.
It is a small part of the "save game" algorithm in a project I'm developing:
Code: Select all
local savePlayer = nil
savePlayer = "player.x = " .. player.x
savePlayer = savePlayer .. "\nplayer.y = " .. player.y
savePlayer = savePlayer .. "\nplayer.level = " .. player.level
savePlayer = savePlayer .. "\nplayer.expToLevel = " .. player.expToLevel
savePlayer = savePlayer .. "\nplayer.exp = " .. player.exp
savePlayer = savePlayer .. "\nplayer.selectedWeapon = " .. player.selectedWeapon
savePlayer = savePlayer .. "\nplayer.maxHealth = " .. player.maxHealth
savePlayer = savePlayer .. "\nplayer.health = " .. player.health
success = love.filesystem.write("player.save", savePlayer)
Basically what it does is to save game variables as a string plus the variable value. (plus = concatenation, not math operation)
Then write it to HDD using love.filesystem.
Next, when you load the saved file, LÖVE will execute everything as if it were a normal Lua module.
You can do it like this:
Code: Select all
ok, chunk = pcall(love.filesystem.load, "player.save" )
chunk()
If you read the LÖVE wiki you will see that loading the file only with love.filesystem.load WILL NOT execute it's contents.
You need to call it manually, that is because in the example I added a
chunk() statement because without it,
chunk will only be a variable with some values assigned (the values you saved into the file previously)