Lua for TIC80

From XPUB & Lens-Based wiki

Like most (imperative) programming languages lua is based on variables, if-then and loop structures, and functions.

Variables & Assignment

See: https://www.lua.org/pil/4.1.html

Remember, unlike what you learned in your algebra class, the single equals sign in an assignment isn't about expressing a long term equality relationship. It's a transactional active structure that does two key things:

  1. evaluate / calculate the right hand side (RHS), then...
  2. store the computed value to a named reference (either a simple variable, or reference inside a table).

Once it's done the code continue and the same variable can be re-assigned to a new value, as in the frequent used case of incrementing a variable.

a = "hello" .. "world"
x = x + 1
t.n = tostring(x)
a.x = 10
-- same as
a["x"] = 10

tables

Tables are the main data structure in Lua.

See:

Tables can be used to store a list of values. The # operator returns the last index (usually, see note below).

squares={1,4,9,16,25,36,49,64,81}

function TIC()
 cls(0)
 for i=1,#squares do
  print(i,i*16,0)
  print(squares[i],i*16,10)
 end
end

Tic80-table.png

Lua tables can also be like python dictionaries (and javascript objects)...

PLANE={
 START_FUEL=2000,
 MAX_FUEL=4000,
 FUEL_INC=1000,
 FUEL_BAR_W=50
}

Tables can be *nested* (one inside the other)...

LVL={
 {
  name="1-1",bg=2,
  palor={},
  pkstart=8,pklen=3,
  mus=BGM.A,
 },
 {
  name="1-2",bg=0,
  palor={[8]=0x102428},
  pkstart=11,pklen=2,
  mus=BGM.B,
 }
}

Examples from: 8-bit panda

More useful examples:

{x=0, y=0}

is equivalent to

{["x"]=0, ["y"]=0}

and the constructor

{"red", "green", "blue"}

is equivalent to

{[1]="red", [2]="green", [3]="blue"}

For those that really want their arrays starting at 0, it is not difficult to write the following:

days = {[0]="Sunday", "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday"}

Finally, you can always use a semicolon instead of a comma in a constructor. We usually reserve semicolons to delimit different sections in a constructor, for instance to separate its list part from its record part:

{x=10, y=45; "one", "two", "three"}

length operator

See: http://www.lua.org/manual/5.1/manual.html#2.5.5

The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte).

The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).

if

See: https://www.lua.org/pil/4.3.1.html

if btn(0) then
 t=t+1
elseif btn(1) then
 t=t-1
end

j94Lj.png

for

Tic80-forloop.png

See: https://www.lua.org/pil/4.3.4.html

function TIC()
 cls(0)
 
 for i=1,100,2 do
  rect(0,0,i,i,i%16)
 end

end

nested loop

??? TO DO ???

while, repeat

Other loop structures are: while and repeat

local i = 1
while a[i] do
  print(a[i])
  i = i + 1
end

functions

See: https://www.lua.org/pil/5.html

break, return

https://www.lua.org/pil/4.4.html