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!)
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
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 |
I wrote my own require
function if you are interestered: https://www.lexaloffle.com/bbs/?tid=140784
@Mimi Not completely sure what you mean, but the include './name_of_file.lua'
works for me.
can you give a simple example of say, using a sprite that is referenced in another lua? I tried that and couldn't get it to work
I pretty much want to use .lua files as "Scriptable objects" and save references to specific things within a lua file and access it from main.lua
in my forsetSpr.lua I might have references to the environment tiles of that zone
include 'GFX/forestSpr.GFX' forestGroundGrass1 = forestSpr.GFX(1) forestGroundGrass2 = forestSpr.GFX(2) forestTree1 = forestSpr.GFX(3) forestTree2 = forestSpr.GFX(4) EnvSprites = [ {'sprite': forestGroundGrass1, 'color': GREEN}, {'sprite': forestGroundGrass2, 'color': GREEN}, {'sprite': forestTree1, 'color': GREEN}, {'sprite': forestTree2, 'color': GREEN} ] |
and then in main.lua I might do something like this (this is obviously pseudo code, just trying to show any use case I might have for this)
include './forestSpr.lua' currentZone = forestLoot.lua --I'm sure there is an actual way to reference it beforehand. I'm new to lua and previously made what I am trying to port in micropython _draw() tileToDraw = random(0, len(forestSpr.EnvSprites)-1) spr(tileToDraw, 64, 64) |
this is a mix of random python and lua hoping someone can help me figure out the correct syntax for using modules like this!
[Please log in to post a comment]