Log In  

Hi! I have been trying to run code from multiple lua files for organization's sake. Since the "require" keyword from lua still seems to not work, I've been trying to figure out other ways. I've been scouring what I could find about picotron and the only way I could figure to import code from another lua file was this, based on what I could understand of the proggy code:

this lists out the dependencies and runs the text in those files as functions as far as I understand it:

here's the matrix lua file:

and here's the folders in the loaded cart:

if anyone else has figured out how to do this in a better way, please let me know because I'm probably doin this in a very silly way. but otherwise, I hope this is helpful!!!

(sorry if this is in the wrong category, picotron isn't showing up for me in the category option for posting, unless i click the post button directly from the cartridges page. edit: I figured out how to post in the right category, I will do that properly next time!)

P#143331 2024-03-16 16:55 ( Edited 2024-03-16 17:06)

1

pico-8 style include works.

Like

include "library.lua"
P#143349 2024-03-16 17:52

noo how did i miss that lol ;o;

thank you haha, I never actually used include from pico-8 so between looking for info about picotron and checking what might've been moved over from pico-8 I guess I missed it

P#143352 2024-03-16 18:08

I found the source code of include here: /system/lib/head.lua:791. It looks like it is using Lua's load function internally. Unfortunatelly include does not return the object returned by the included file which is a shame, because it could be extremely useful in supporting real Lua modules.

function include(filename)
        local filename = fullpath(filename)
        local src = fetch(filename)

        if (type(src) ~= "string") then 
            notify("could not include "..filename)
            stop()
            return
        end

        -- https://www.lua.org/manual/5.4/manual.html#pdf-load
        -- chunk name (for error reporting), mode ("t" for text only -- no binary chunk loading), _ENV upvalue
        -- @ is a special character that tells debugger the string is a filename
        local func,err = load(src, "@"..filename, "t", _ENV)

        -- syntax error while loading
        if (not func) then 
            -- printh("** syntax error in "..filename..": "..tostr(err))
            --notify("syntax error in "..filename.."\n"..tostr(err))
            send_message(3, {event="report_error", content = "*syntax error"})
            send_message(3, {event="report_error", content = tostr(err)})

            stop()
            return
        end

        -- call directly; allows complete stacktrace -- runtime error can be handled in general case
        func()

        return true -- ok, no error including
    end
P#143477 2024-03-17 10:45 ( Edited 2024-03-17 10:48)

I wrote my own require function if you are interestered: https://www.lexaloffle.com/bbs/?tid=140784

P#143482 2024-03-17 11:33

[Please log in to post a comment]