Ok, this is embarassing. I never learned about tables in Codea. Never how to use them, because the demo in the referance manual doesnt have a demo, just a lot of confusing sentences. And i need to learn them. Any tutorials focusing on specificaly(Is that how you spell it? Let me know that also) would be much appreciated, Thanks!
I have a chapter in here specifically for you.and don’t be embarrassed - it took me a while to learn tables because as you say, it’s hard to find good tutorials
https://www.dropbox.com/s/qh1e7ft4rvhlpt2/Lua%20for%20beginners.pdf
@CodeaTutorials Let me try to explain tutorials in a easy way.
As you probably know, a table is basically tons of variables that in a “box”
Lets say we named our box: objects = {}
Then we could add stuff to “objects” to ways, we could either put the things in the brackets followed by a comma like this: objects = {vec2(5,5), “easteregg”} .
Or you could do: table.insert(objects, vec2(5,5))
and that would insert a vector2 into the table. Now you are probably wondering how to use these variables now. First, you need to find the slot its in, for example, if I have: objects = {“Hi”, “bye”, “hello”} Then I could get the “hello” string by typing objects[2] Because its in the second slot. Or if its a vec2 you would do:
objects[a number].x or objects[a number].y . If you are wanting to use tables for fire bolts or something that has to be created and drawn on the screen, then you would loop through them like this:
for i=1, #objects do
sprite(“bullet”, objects[i].x, objects[i].y
end
Let me explain the above code, first we are looping through the table, as you probably know. But you are probably wondering why we have a “#” in-front of the table name, basically when you do this, it gives you the number of things in a table, very useful for things like this. And when we do objects[i].x, we are drawing a sprite on i, which goes through the entire table once every frame.
I hope I explained it well enough and you can understand.
You’ve probably figured it all out by now… But a while ago. I made a slideshow on tables:
-- Tables
-- Use this function to perform your initial setup
function setup()
math.randomseed(0)
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
lastChange = 0
cam = {x = 0, y = 0, size = 45}
tween.delay(5, function()
anim({y = -HEIGHT}, 6, {y = -(2 * HEIGHT)}, 5, {y = -(3 * HEIGHT)}, 8, {y = -(4 * HEIGHT)}, 8, {y = -(5 * HEIGHT)}, 22, {y = -(6 * HEIGHT)}, 12, {y = -(7 * HEIGHT)}, 16, {y = -(8 * HEIGHT)}, 26, {y = -(9 * HEIGHT)}, 5)
end)
parameter.watch("cam.x + (2 * (ElapsedTime - lastChange))")
parameter.watch("cam.x")
parameter.watch("cam.y")
parameter.watch("cam.size")
parameter.watch("modelMatrix()")
parameter.watch("viewMatrix()")
parameter.watch("projectionMatrix()")
end
function anim(...)
a(1, arg[1], arg)
end
function a(k, v, i)
lastChange = ElapsedTime
local j = v
j["x"] = math.random(0, 50)
tween(1, cam, j, tween.easing.sineInOut, function()
tween.delay(i[k + 1], function()
if i[k + 2] ~= nil then
a(k + 2, i[k + 2], i)
end
end)
end)
end
-- This function gets called once every frame
function draw()
-- This sets a light background color
background(255)
-- This sets the line thickness
strokeWidth(3)
-- Do your drawing here
font("AmericanTypewriter")
fontSize(24)
fill(0)
stroke(0)
perspective(cam.size)
camera(cam.x + (2 * (ElapsedTime - lastChange)), cam.y, WIDTH - 100, 0, cam.y, 0, 0, 1, 0)
textMode(CENTER)
translate(0, 0, 0)
text("Let's talk about tables.")
shift()
text("A table can store multiple variables,\
all wrapped together in one variable.")
shift()
text("It looks like this:")
text("{\"a\", \"b\", \"c\"}", 0, -36)
shift()
text("{\"a\", \"b\", \"c\"}")
line(-150, -150, -48, -18)
line(0, -200, 0, -18)
line(150, -150, 48, -18)
text("1st element", -150, -165)
text("2nd element", 0, -215)
text("3rd element", 150, -165)
shift()
fontSize(18)
text("You can access an element by doing something like this:")
fontSize(24)
text("table[element]", 0, -36)
shift()
fontSize(18)
text("Example:")
fontSize(24)
text("myTable = {1, 2, 3, \"a\", \"b\", \"c\"}", 0, -36)
text("print(myTable[4])", 0, -66)
fontSize(18)
text("This will print \"a\", the fourth element in myTable.", 0, -102)
strokeWidth(2)
line(62, -20, 62, 60)
strokeWidth(3)
text("#4", 62, 72)
shift()
fontSize(24)
text("Tables can also store named variables in them.")
text("myTable = {abc = \"123abc\"}", 0, -36)
shift()
text("The \"abc\" variable inside myTable can be accessed")
text("with either myTable.abc or myTable[\"abc\"].", 0, -36)
shift()
text("There are also other built-in table functions")
text("Such as table.insert(table, value) will add", 0, -36)
text("the value you supplied to the end of the table.", 0, -66)
text("There are many other table functions, but we won't go over them now.", 0, -96)
shift()
text("Have fun playing with tables!")
resetMatrix()
end
function shift()
translate(0, -HEIGHT, 0)
end