Open atesin opened 9 months ago
require
simple isn't supported. All your code has to be placed in the single main file.
require
simple isn't supported. All your code has to be placed in the single main file.
I may misunderstand, but I think it does, see #2352
I never used it, not sure I can help but I try anyway:
Did you look at https://github.com/nesbox/TIC-80/wiki/Using-require-to-load-external-code-into-your-cart ? From what I understand the libraries should be in TIC-80/lua/module.lua
file.
Also, I think current directory in TIC-80 is always TIC-80
and not where your cart is or where you cd
#775
However, note that the use of require
could be disabled in future update: #2352
require
simple isn't supported. All your code has to be placed in the single main file.
not true, i tried with some of dirs shown in error message and they worked... the problem reported here is require
doesn't work with files in tic80 command console current working directory specifically
it seems like tic80 embedded lua only sees the folder where tic80.exe
executable is, and doesn't know/care about tic80 console virtual environment, so the required files are not found
Did you look at https://github.com/nesbox/TIC-80/wiki/Using-require-to-load-external-code-into-your-cart ? From what I understand the libraries should be in
TIC-80/lua/module.lua
file.
yes i tried what that wiki page says and doesn't work (maybe worked in a previous tic80 version)... and for regular lua files other than modules or libraries (e.g. grouplng similar functions, etc.) should work in tic80 command console "cwd" (current working directory, shown in prompt synbol), or the dir where current cart were loaded
Also, I think current directory in TIC-80 is always
TIC-80
and not where your cart is or where youcd
#775
i agree with @moonlit in that this behavior is confusional, prone to provoke errors and should be fixed
However, note that the use of
require
could be disabled in future update: #2352
require
lua function could be very useful, i think it should be fixed in its security issues (the right way) instead being disabled (the fast and lazy way)... check my comment there for more details: https://github.com/nesbox/TIC-80/issues/2352#issuecomment-1871701386
i am using this dirty workaround (it bothers, should load files in same directory without the need of doing these tricks):
<tic80-install>/project-name
project-main.lua
, functions.lua
project-main.lua
is the file that has header comments, TIC()
function, and others<tic80-carts>/project-cart.tic
with just this content:
require('project-name.functions') -- will map to "<tic80-install>/project-name/functions.lua"
require('project-name.project-main') -- main file at LAST because include also execute files
<tic80-install>/project-name
with zerobrane or another external editorimport code joined-files.lua
, and saveAs i said originally:
require simply isn't supported.
not true, i tried with some of dirs shown in error message and they worked..
There is a huge difference between "it works" and "it is supported". I'll repeat: This is not supported - it should never work, the existing behavior is a bug. Some people wish it was [supported] (there is a ticket for this), but it's currently not. And there is also a ticket to "break" it fully as it's a security risk - and not supported. If require simply never worked period, all the confusion would be gone.
@joshgoebel ok
the behaviour indeed can be a bit erratic: for some computers, i've had to add the following line to my main.lua
for require to work:
package.path = package.path..";C:/path/to/your/directory/?.lua"
It adds your working directory to Lua's package.path so Lua can find your files.
@borbware : as i thought config.cart
is loaded frist any cartridge, i tried to set this permanently by typing config
command and adding this line in code editor:
package.path='%APPDATA%/com.nesbox.tic/TIC-80/?.lua;%APPDATA%/com.nesbox.tic/TIC-80/?/init.lua;'..package.path
THEME=
{
CODE =
{
...
...
but didn't worked :( ... is there any way to do this permanently? (i think it should come from stock)
@TimotheeGreg : i think the instructions in the page you posted are wrong ( https://github.com/nesbox/TIC-80/wiki/Using-require-to-load-external-code-into-your-cart#where-to-locate-our-module-files )
there it says "Open up tic80 and at the console type folder" and create a lua
subfolder there, but after using require()
with a non existing file, the traceback shows me all paths lua tried, but doesn't show lua
subfolder inside tic80 carts folder, but instead it shows a lua
subfolder inside tic80.exe
executable folder... changing the start folder in tic80 shorccut didn't worked either
(edited after replies to add images)
i found a better workaround that works:
as a response to my previous response @TimotheeGreg , i put together these 2 facts:
%APPDATA%/com.nesbox.tic/TIC-80
.\?.lua
and .\?\init.lua
(universally ".\" stands for "current folder")when i edit tic80.exe shortcut "start folder" as shown above, and add require('req-test')
to cart code editor, external edition works for lua files (carts_folder)/req-test.lua
and (carts_folder)/req-test/init.lua
=D
... you can trace full expanded package.path
by requiring a nonexistant file as shown above, so after analyzing we realize we can use .\?.lua
to do nice things like group cart includes in folders
cart-libs\greet
-> %START_FOLDER%\cart-libs\greet.luacart-libs\farewell
-> %START_FOLDER%\cart-libs\farewell.luaor just use .\?\init.lua
to add require("cart-folder")
in tic80 code editor, and have cart-folder\\init.lua
to manage all dependencies and codes externally
obviously this should be made only in development stage, once the project has done we must put it all together inside cart
TIC-80 should really support some form of multi-file support. It need not be the native require
function, but I think should be something that would allow people to easily refactor their require
hacks to use the new solution for their previous sandbox-friendly needs.
require
-like syntaxedit <otherfile>
support, or similarI don't think this violates the retro spirit of TIC-80 more than the editor outline and bookmarks views do already and I'm not sure what use the console (e.g. mkdir) is without this sort of feature.
EDIT: i found a workaround in https://github.com/nesbox/TIC-80/issues/2426#issuecomment-1879215018 , please dont nerf it
hi, i am in the programming developing/learning stage
to keep some order while in developing/testing i pretend to separate the program in various lua files, and call them with
require('my-lib')
in my cart code... previously i created a subfolder inside tic80 carts folder, to keep order i pretend later to create a separate folder for each project, with cart and all needed files insidecurrently my folder structure looks like
in tic80 console i must type these commands
ahd the code looks like this (for later production, i can simply concat all lua files, import it to cart and delete these
require
lines)but when i try to run the cart, it crashes and these messages appear in console
... i can clearly see the line
no file '.\oop.lua'
, but i swear this file exists as.\
stands for "current directory", and current directory inside tic80 is<cart folder>\001-oop
as i recently cd'ed and console prompt showsi think it must be a bug, that a required file exists in current directory, and current directory being expanded to tic80 console's current directory, by default, as console prompt shows .... but i see currently is not the case :( ... for user intuitiveness and ease it SHOULD be the default behavior
( see this issue also, is somewhat related: #2414 , maybe all these complications with files won't be necessary if editor would be multi-tabbed :) )
could you please fix this bug / correct this oddity / add this feature, in the future releases? ... thank you