Logo Signal From The Stars

Save

Save and load

Martin avatar
  • Martin
  • 4 min read
Imagine a game without a save game...

Save

For some reason I didn’t want to work on this right away, in fact I was afraid that this would be a difficult thing. But since I had already made some test scenes it was easier to save the game. And of course load it.

For this I created a class in engine3, namely serialization.lua, this is a wrapper around binser.

This makes it possible to save object data correctly.

In the game I then created a state.lua class that looks something like this.

function State:saveEntities()
    for _, entity in pairs(entityManager.entities) do
        if entity.saveEntity then
            entity:saveEntity()
        end
    end
end

---@param file string Where to save the data
function State:save(file, force)
    -- Check if it is possible to save right now
    if action:isProcessWorking() and force ~= true then
        print("saving is not possible now, a action is still working")
        return false
    end

    local data = {}

    -- Create a save state for all entities that are needed
    self:saveEntities()

    -- We don't need all session data.
    -- @warning keep this up to date if you want to add something
    local sessionData = session:getData()
    local validSessionKeyNamePatterns = {
        "^actor-.*",
        "^item-.*",
        "^animation-.*",
        "^scene-.*",
        "action",
        "^entity_delete",
        "^entity_properties",
        "^currentSceneUkey"
    }
    for sessionKeyName, sessionValue in pairs(sessionData) do
        -- I don't know how to create multi patterns ... [x|y] don't work
        local check = false
        for _, pattern in pairs(validSessionKeyNamePatterns) do
            if string.match(sessionKeyName, pattern) ~= nil then
                if self.config.log.state then
                    print("[SESSION.SET." .. sessionKeyName .. "]")
                end
                data[sessionKeyName] = sessionValue
                check = true
                break
            end
        end

        if check == false and self.config.log.state then
            print("[SESSION.INFO." .. sessionKeyName .. "] don't match any save pattern")
        end
    end

    local success, message = love.filesystem.write(file, Serialization.serialize(data))
    if success == false then
        error("Cannot save data, " .. message)
    end
end

---@param file string What file data to load
function State:load(file)
    local data = love.filesystem.read(file)
    if data then
        local results, len = Serialization.deserialize(data)
        -- at the moment we only use one object in each file
        if len == 1 then
            -- set the session data
            session:setData(results[1])
        end
    end
end

Usage

In each entity class, two functions can now be created (optionally) that are used during saving and loading of the game. Below you see a concise example of the characters entity. The advantage of this approach is that you can go in all directions in the future, but also that the files are very small.

-- special function that will execute when saving the game
function Actor:saveEntity()
    -- maybe a extend class did create some data
    local data = session:get("actor-" .. self.ukey) or {}

    data.sceneUkey = self.sceneUkey
    data.lastMove = self.lastMove
    data.movableByUserInput = self.movableByUserInput

    if self.activeFrameset then
        data.startPositionX = self.activeFrameset:getX()
        data.startPositionY = self.activeFrameset:getY()
    end

    -- maybe the actor did change the frameset (using a hat)
    data.frameset = {}
    for index, frameset in pairs(self.frameset) do
        data.frameset[index] = frameset.name
    end

    session:set("actor-" .. self.ukey, data)
end

function Actor:loadEntity()
    local restoreData = session:get("actor-" .. self.ukey)
    if restoreData == nil then
        return
    end

    if restoreData.sceneUkey ~= nil then
        self:setSceneUkey(restoreData.sceneUkey)
    end

    if restoreData.movableByUserInput ~= nil then
        self:setMovableByUserInput(restoreData.movableByUserInput)
    end

    -- maybe the actor did change the frameset (using a hat)
    if restoreData.frameset then
        for index, framesetName in pairs(restoreData.frameset) do
            self:setFrameset(index, framesetName)
        end
    end

    if restoreData.startPositionX and restoreData.startPositionY then
        self.movable.vPosition.x = restoreData.startPositionX
        self.movable.vPosition.y = restoreData.startPositionY
        self:setXY(self.movable.vPosition.x, self.movable.vPosition.y)
    end

    if restoreData.lastMove then
        -- what will be the new activeFrameset (index)
        self:setActiveFrameset(restoreData.lastMove)
    end

    -- when load scene is used, we don't set the player position
    -- @warning we do this after setXY, because that function will show the player
    local currentSceneUkey = session:get("currentSceneUkey")
    if currentSceneUkey and self.sceneUkey ~= currentSceneUkey then
        self:hide()
    end
end

Finally

As you can see, you only need to make sure that you save the data that you actually need. All other data is redundant and makes debugging almost impossible. Don’t wait too long with such a system, since you will have to make it anyway.

The code above is currently controlled by a button, with a screenshot of the game and the chosen character saying that the save was successful.

self.buttonSave.callbackAfterMousePressedEvent["btnSave"] = function(button)
        local currentTime = os.time()
        local saveGameName = "savegame" .. currentTime .. ".data"
        local saveGameScreenshot = "screenshot" .. currentTime .. ".png"

        -- create a screenshot without the state menu (write it to the save directory (love default))
        love.graphics.captureScreenshot(saveGameScreenshot)

        -- save the game
        State():save(saveGameName)

        -- say that the save the game was correct
        local activeActor = entityManager:find({ movableByUserInput = true }, "Player")
        if #activeActor == 1 then
            local actionSay = ActionSay()
            actionSay:execute(
                {
                    action = action.SAY,
                    initiator = activeActor[1]
                },
                {
                    text = i18n:s("Save game done!")
                },
                nil,
                {},
                {},
                nil
            )
        end

        self.buttonLoad:setIsVisible(true)

        return { preventDefault = true }
    end

Loading external comment system...

βœ‰οΈ Stay informed!

Receive the latest news for free and motivate me to make this adventure a success!