Log In  

Hey, hello! I'm a beginner in programming. I followed the tutorial "Game Development using PICO-8 by Dylan Bennett" to learn to use Pico-8. follow the page 28 of the tutorial. I wrote a game which just like "Flappy Bird", and then I ran into some questions about "tables".

The questions are as follows:
I don't understand what the means about"cave={{["top"]=5,["btm"]=119}}".

The context of this code is as follows(In line 2):

function make_cave()
    cave={{["top"]=5,["btm"]=119}}
    top=45
    btm=85
end

function update_cave()
    if (#cave>player.speed) then
        for i=1,player.speed do 
        del (cave,cave[1])
        end
    end

    while (#cave<128) do
    local col={}
    local up=flr(rnd(7)-3)
    local dwn=flr(rnd(7)-3)
    col.top=mid(3,
    cave[#cave].top+up,top)
    col.btm=mid(btm,
    cave[#cave].btm+dwn,124)
    add(cave,col)
    end

end

function draw_cave()
    top_color=5
    btm_color=5
    for i=1,#cave do
        line(i-1,0,i-1,cave[i].top,
        top_color)
        line(i-1,127,i-1,cave[i].btm,
        btm_color)
    end
end
P#105969 2022-01-30 16:38

1

Basically when you have double curly braces like that, it means "a table of tables". You're making a table where the items in the table are made up of... more tables. So this code...

things = {
 {5, 6},
 {9, 4},
 {3, 4}
}

...would mean you have a table called things that has 3 items in it. Each item is a table. Those tables each have their own 2 items (the numbers). I hope that makes sense.

P#105980 2022-01-30 17:17
1

some info in in official doc: https://www.lexaloffle.com/dl/docs/pico-8_manual.html#Tables
more in books like programming in lua (read the version for lua 5.2, which pico8 is based on)

-- tables are data structures that associate keys and values
t1 = {}
t1["a"] = "keys can be strings"
t1[true] = "keys can be other types!"
t1.b = "string keys have this shortcut notation"

t2 = {
 ["a"]="create table and contents in one go",
 [true]="just need these brackets before = sign",
 b="unless you use this shortcut for strings!  no bracket or quotes but same result",
}

-- tables can also be used to store an ordered list of things
t3 = {}
add(t3, 10)
add(t3, 20)
add(t3, 30)
del(t3, 20)
# the functions automatically give keys 1,2,3 etc for the values
# t3[1] == 10 and t3[2] == 30 !

t4 = { 10, 20, 30, 20, 12 }  -- create table and contents in one go

-- it is common to mix them!
all_goblins = {}
goblin1 = { x=10, y=10, health=21 }
add(all_goblins, goblin1)
goblin2 = { x=40, y=10, health=42 }
add(add_goblins, goblin2)

-- this one is also a table-as-sequence of tables-as-objects (with key-values)
fairies = {{sp=10}, {sp=11}, {sp=12}}
P#106010 2022-01-30 22:06

Oh! thank you all. It's really fun to figure out some things!

P#106040 2022-01-31 12:46

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-20 00:19:53 | 0.007s | Q:14