πŸ‘ƒπŸ““

Pico8 Lua cheatsheet

22 Oct 2017

Pico8 Lua Cheetsheet

Comments

-- use two hyphens like this to ignore everything until the end of the line
--[[ multi-line
comments ]]

Types and assignment

Types in Lua are numbers, strings, booleans and tables;

NUM = 12/100
S = "THIS IS A STRING"
B = FALSE
T = {1,2,3}

Conditionals

IF NOT B THEN
	PRINT("B IS FALSE")
ELSE
	PRINT("B IS NOT FALSE")
END

-- with ELSE-IF

IF X==0 THEN
	PRINT("X IS 0")
ELSEIF X < 0 THEN
	PRINT("X IS NEGATIVE")
ELSE
	PRINT("IF NONE OF THE ABOVE")
END

IF (4 == 4) THEN PRINT("EQUAL") END IF (4 != 3) THEN PRINT("NOT EQUAL") END -- note that != can also can be written as ~=

LOOPS

FOR X=1,5 DO
	PRINT(X)
END

FOR X=1,10,3 DO PRINT(X) END   -- 1,4,7,10

FUNCTIONS

Y=0
FUNCTION PLUSONE(X)
	LOCAL Y = X+1
	RETURN Y
END
PRINT(PLUSONE(2)) -- 3
PRINT(Y) 	  -- 0

TABLES

A={} -- create an empty table
A[1] = "BLAH"
A[2] = 42
A["FOO"] = {1,2,3}

-- Arrays use 1-based indexing by default

A = {11,12,13,14}
PRINT(A[2]) -- 12

-- size of a table indexed with sequential 1-based integers

PRINT(#A) -- 4

-- Indexes that are strings can be written using dot notation

PLAYER = {}
PLAYER.X = 2 -- equivalent to PLAYER["X"]
PLAYER.Y = 3

GENERAL PICO8 CARTRIDGE NOTES

-- OCEAN DIVER LEGENDS
-- BY LOOPY

Additional PICO8 Commandline notes

folder - open the carts folder in host operating system
ls - same as Bash
run - run from start of program. call inside a program to reset it.
reboot - useful for starting new project
printh str [filename] - print a string to host operating system's console for debugging

Program Structure