Lua for TIC80: Difference between revisions

From XPUB & Lens-Based wiki
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
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:
# evaluate / calculate the right hand side (RHS), then...
# 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.
<source lang="lua">
a = "hello" .. "world"
x = x + 1
t.n = tostring(x)
</source>
<source lang="lua">
a.x = 10
-- same as
a["x"] = 10
</source>
== tables ==
== tables ==
Tables are the main data structure in Lua.
See:
* [https://www.lua.org/pil/3.6.html Table Constructors]
* https://www.lua.org/pil/11.html


[[File::tic80-table.png]]
Tables can be used to store a list of values. The # operator returns the last index (usually, see note below).


<source lang="lua">
<source lang="lua">
squares={1,4,9,25,36,49,64,81}
squares={1,4,9,16,25,36,49,64,81}
 
function TIC()
function TIC()
  cls(0)
  cls(0)
Line 13: Line 44:
end
end
</source>
</source>
[[File:tic80-table.png]]
Lua tables can also be like python dictionaries (and javascript objects)...
<source lang="lua">
PLANE={
START_FUEL=2000,
MAX_FUEL=4000,
FUEL_INC=1000,
FUEL_BAR_W=50
}
</source>
Tables can be *nested* (one inside the other)...
<source lang="lua">
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,
}
}
</source>
Examples from: [https://github.com/btco/panda/blob/master/source/panda.lua 8-bit panda]
More useful examples:
<source lang="lua">
{x=0, y=0}
</source>
is equivalent to
<source lang="lua">
{["x"]=0, ["y"]=0}
</source>
and the constructor
<source lang="lua">
{"red", "green", "blue"}
</source>
is equivalent to
<source lang="lua">
{[1]="red", [2]="green", [3]="blue"}
</source>
For those that really want their arrays starting at 0, it is not difficult to write the following:
<source lang="lua">
days = {[0]="Sunday", "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday"}
</source>
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:
<source lang="lua">
{x=10, y=45; "one", "two", "three"}
</source>
== 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 ==
== if ==
Line 45: Line 155:
</source>
</source>


== function ==
=== nested loop ===
 
??? TO DO ???
 
== while, repeat ==
 
Other loop structures are: [https://www.lua.org/pil/4.3.2.html while] and [https://www.lua.org/pil/4.3.3.html repeat]
 
<source lang="lua">
local i = 1
while a[i] do
  print(a[i])
  i = i + 1
end
</source>
 
== functions ==
 
See: https://www.lua.org/pil/5.html


== break, return ==
== break, return ==
https://www.lua.org/pil/4.4.html
https://www.lua.org/pil/4.4.html

Latest revision as of 15:23, 1 February 2021

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