-
-
Notifications
You must be signed in to change notification settings - Fork 490
Using require to load external code into your cart
If you like to break out your source code into separate files during development you can use the Lua require function to import the code. Using the module example mymath
from Lua - Modules we can do the following.
Below is the code for the module from the Lua website in Lua
and Fennel
-- Lua module
local mymath = {}
function mymath.add(a,b)
print(a+b)
end
function mymath.sub(a,b)
print(a-b)
end
function mymath.mul(a,b)
print(a*b)
end
function mymath.div(a,b)
print(a/b)
end
return mymath
;; Fennel module
(local mymath {})
(fn mymath.add [a b]
(print (+ a b)))
(fn mymath.sub [a b]
(print (- a b)))
(fn mymath.mul [a b]
(print (* a b)))
(fn mymath.div [a b]
(print (/ a b)))
mymath
We will use the demo cart here and just add some additional lines:
-- script: lua
mymath = require("mymath")
t=0
x=96
y=24
function TIC()
if btn(0) then y=y-1 end
if btn(1) then y=y+1 end
if btn(2) then x=x-1 end
if btn(3) then x=x+1 end
cls(13)
spr(1+t%60//30*2,x,y,14,3,0,0,2,2)
print("HELLO WORLD!",84,84)
mymath.add(1,1) -- call the function from the module which will print the result to screen
t=t+1
end
;; script: fennel
;; strict: true
(local mymath (require "mymath"))
(var t 0)
(var x 96)
(var y 24)
(fn _G.TIC []
(when (btn 0) (set y (- y 1)))
(when (btn 1) (set y (+ y 1)))
(when (btn 2) (set x (- x 1)))
(when (btn 3) (set x (+ x 1)))
(cls 0)
(spr (+ 1 (* (// (% t 60) 30) 2))
x y 14 3 0 0 2 2)
(print "HELLO WORLD!" 84 84)
(mymath.add 1 1) ;; call the function from the module which will print the result to screen
(set t (+ t 1)))
- Open up
tic80
and at the console typefolder
. - This will show you where the tic80 data folder is.
- Create a folder inside this one named
lua
- Place your
module.lua
ormodule.fnl
file here. This is where tic80 looks for the files.
Just run your cart now and you should see the mymath.add
print the sum of the arguments you supplied
You need to have access to the external files for the cart to work. If tic80 can't find the files the cart will fail. For example if you export to Windows .exe
you will need to make sure that you also supply the lua
directory that includes your modules with the .exe
file. You can't just ship the .exe
alone.
This may not be an issue for you but it is something to be aware of.
When sharing your cart, you could copy the contents of the files into your cart at the end, so its 1 giant cart.
TIC-80 tiny computer https://tic80.com | Twitter | Telegram | Terms
Built-in Editors
Console
Platform
RAM & VRAM | Display | Palette | Bits per Pixel (BPP) |
.tic
Format | Supported Languages
Other
Tutorials | Code Snippets | Libraries | External Tools | FFT
API
- BDR (0.90)
- BOOT (1.0)
- MENU
- OVR (deprecated)
- SCN (deprecated)
- TIC
- btn & btnp
- circ & circb
- clip
- cls
- elli & ellib (0.90)
- exit
- fget & fset (0.80)
- font
- key & keyp
- line
- map
- memcpy & memset
- mget & mset
- mouse
- music
- peek, peek4
- peek1, peek2 (1.0)
- pix
- pmem
- poke, poke4
- poke1, poke2 (1.0)
- rect & rectb
- reset
- sfx
- spr
- sync
- ttri (1.0)
- time
- trace
- tri & trib (0.90)
- tstamp (0.80)
- vbank (1.0)