log #5: saves

Time interval: 2023-02-10/2023-02-18

It’s been a bit of time since the last post, but that’s because I spent last week working on my submission to the nokia jam 5. The things you will see here are actually from before the jam, I just forgot to make a post about them.

saving the current world

My main task was saving and loading the current world. This turned out to be a bit harder than I anticipated. The reason for this is the ctx field all my entities have. This field is of type any and is used to store data the entity needs to function. This field is usually initialized in the transformer functions I talked about in log 1, but sometimes it might be needed to keep the data between different sessions.

I came up with a similar solution to the transformer functions. Every entity that needs to save it’s ctx defines it’s own load and export functions. For example the book entity:

fn exportBook*(e: world.Ent, ctx: any): str {
    this := book.Book(e.ctx)

    o := "["
    for i,p in this.pages {
        o += sprintf("\"%s\"", p)
        if i < len(this.pages) - 1 { o += "," }
    }

    return o + "]"
}

fn loadBook*(e: ^world.Ent, m: any, ctx: any): any {
    b := book.Book{}
    a := []any(m)
    for i in a {
        b.pages = append(b.pages, str(a[i]))
    }

    return b
}

Then I just register the functions along with the tranformer:

    res.registerEnt("book", e_book, null)
    res.registerEntExporter("book", exportBook, null)
    res.registerEntLoader("book", loadBook, null)

I also added train saving and player position saving. Saving schedules still doesn’t work properly as the schedule item index is reset on every load.

Each individual savegame has it’s own folder with a save.json file providing meta information about the save. All the saves are then stored in a saves.json file.

pause menu

Since I made the saves, I needed a way to save the game. In the past I used a simple input.isJustPressed call, but I decided to make a proper pause menu instead.

gif of the new pause menu

Clicking the exit button will return you back to the save manager.

save manager

The save manager is the last thing I added. It allows you to create, load and delete saves. Saves are created from template saves, which are fully functional saves of their own. The created saves will copy some of the resources used by the template save, but will also share some resources with the base save. This way the base saves can be updated and the user saves will be updated too.

gif showing the new save manager