Log In  

hi there

edit: i've updated this a bit and put this into an installer cartridge (here) which can also be installed via yotta, a package and dependency manager.

i've already wanted to throw out what i'm working on and start a cart from scratch a few times and am accustomed to being able to use PICO-8's reset to do so. this doesn't seem to work well here, and i don't like just closing and reopening picotron. we have save and load but not reset or new. so... i made new.

you can place this code in /appdata/system/util/new.lua:

-- remove current cart and replace with template cart
source_template = "/appdata/new/template.p64"
default_filename = "/untitled.p64"

if(fstat("/appdata/new") != "folder") mkdir("/appdata/new")

-- (most of this copied from /system/util/load.lua, thx zep)

-- remove currently loaded cartridge
rm("/ram/cart")

-- create new one
local result = cp(source_template, "/ram/cart/")
if (result) then
    print(result)
    exit(1)
end

-- set current project filename
store("/ram/system/pwc.pod", default_filename)

-- tell window manager to clear out all workspaces
send_message(3, {event="clear_project_workspaces"})

dat = fetch_metadata("/ram/cart")
if (dat) dat = dat.workspaces

-- create workspaces
if (type(dat) == "table") then
    -- open in background (don't show in workspace)
    local edit_argv = {"-b"}

    for i=1,#dat do
        local ti = dat[i]
        local location = ti.location
        if (location) then
            add(edit_argv, "/ram/cart/"..location)
        end
    end

    -- open all at once
    create_process("/system/util/open.lua",
        {
            argv = edit_argv,
            pwd = "/ram/cart"
        }
    )
end

print("new cart initialized.")

once /appdata/system/util/new.lua exists, you can immediately invoke it on the terminal by simply typing new. any lua script in this util directory behaves like this, as noted in the readme. i love this.

you'll need a .p64 cart saved that will serve as your "new cart" template. you can customize where to put it, but by default it expects you'll save one at /appdata/new/template.p64. to make this, i suggest freshly booting up picotron, editing the cart and code to whatever you'd like your default cart to look and act like, making the directory (mkdir /appdata/new) and saving it directly (save /appdata/new/template.p64). you can edit anything and it will be carried over to newly-created carts (via new, anyway) - sprites, maps, sfx, multiple luas, even cover art, etc.

if you customize default_filename up at the top, it will change where it saves the cart to if you were to invoke new then make some edits and just hit ctrl+s without manually invoking save <filename>. i've moved this to be /dev/untitled.p64 because i like to hide my clutter away.

warning: it will simply destroy any unsaved changes in the current cart. i would like to find out if there is a way to detect "unsavedness" in the future and prompt/warn about it and maybe require an overwrite flag if the workspace is dirty... but i'm not sure how to do that yet. so, take care.

it looks like /appdata/system/startup.lua can be used to run custom startup code. in here, i would probably invoke this 'new' script, so that when i boot picotron it starts as my template. i haven't done this yet, because i am breaking other things ... but i think i will shortly.

if you're looking for ideas, my template only has a modification of main.lua to be the following:

-- New Picotron Cartridge
-- by Your Name Here

-- called once per cart run
function _init()

end

-- called every frame
function _draw()

end

-- called every 1/60th of a second
function _update()

end
P#143060 2024-03-15 05:52 ( Edited 2024-03-18 01:26)

Thank you! That helps a lot!

P#143167 2024-03-15 17:38

This is exactly what I've been searching for. Thanks!

P#143223 2024-03-15 23:07
1

i later replaced the default_filename constant with something dynamic, since i found myself losing some temporary "sketches".

the below code replaces the first 3 lines of the original script, and will automatically name your new cartridge "/untitled_YYYYMMDD_NN.p64" - where YYYYMMDD is a current datestamp and NN is an incrementing number starting at 0. if, when you run new, /untitled_YYYYMMDD_00.p64 already exists, your new cart will have /untitled_YYYYMMDD_01.p64 baked-in as its filename. this way, you can quickly new something, make some tweaks and test some things, hit ctrl+s to keep it around, then just hit up new again without risk of blowing up what you just saved. is it messy? yes. i'm a little messy.

-- source template cart (must exist!)
source_template = "/appdata/new/template.p64"

if(fstat("/appdata/new") != "folder") mkdir("/appdata/new")

-- determine useful default filename
today = table.concat(split(split(date(), " ")[1], "-", false))

default_filename_prefix = "/untitled_" .. today .. "_"
default_filename = default_filename_prefix .. "00.p64"

inst = 0
while fstat(default_filename) do
    inst += 1

    sinst = inst
    if(inst<10) sinst = "0"..inst

    default_filename = default_filename_prefix .. sinst .. ".p64"
end

-- ... (remainder of existing script continues here) ...
P#143370 2024-03-16 21:13 ( Edited 2024-03-18 01:03)

I've been using this a lot, it's a great tool! a small suggestion: make an appdata folder for your tool and put the template there, to keep the system tidy. I've changed mine to load from source_template = "/appdata/new/template.p64"

P#143604 2024-03-18 00:33
1

@pancelor that's a fantastic idea, I'll do so. After pulling my head out of a bunch of initial development, I've noticed others on the BBS are doing this. I'll make the edits to this, because that keeps everyone's systems cleaner :)

P#143612 2024-03-18 01:02

[Please log in to post a comment]