Log In  

I've been trying to wrap my head around saving and loading in Picotron.

As always feel free to correct or improve, I'm always happy to learn.

2 main concepts to understand

As far as I know there is no automatic allocation of space for persistent userdata like in Pico8. You save your userdata as you would in other conventional game engines as an extra file. This makes it a whole lot easier.

This data is best stored as a table, so try player_vars={"Tammo",{"Sword",1},}. That data structure is internally stored as a .pod, which can be saved or read via store and fetch respectively. (Otherwise you'll have to arrange your data and pod it before saving just to unpod it after loading it.

Now you'll just need a place to store your data. It is good practice to create your own folder inside /appdata with mkdir. For example mkdir(save_dir)

These files can be opened and read, but this is fitting for our open community. Let's imagine the easiest possible usecase. We want to keep track of the highscore. We need to check for any existing high scores on _init and load them if they exist. If not we'll just start with the default value.
Here are the functions needed, tune them to your usecase:

function save_game()
local err=mkdir(save_dir)
if err!=nil then
print(err)
else
print("worked")
end
store(save_dir.."/"..save_file,player_vars)
end

function load_game()
local presave
presave=fetch(save_dir.."/"..save_file)
    if presave!=nil then
        print("loading succesfull")
        player_vars=presave 
    else
        print("no save file detected")
    end
end

These functions need both a save_dir and a save_file declared in your _init to be globally accessible.

    save_dir="/appdata/PicoWings"
    save_file="highscore.pod"

In this case we also need our player vars:

player_vars={high_score}

And please do correct me if I got it all wrong. Have a nice day!

P#145887 2024-04-06 13:02 ( Edited 2024-04-06 14:09)


[Please log in to post a comment]