0

So here's the thing, i'm trying to make a fully customizable UI in computercraft, using lua arrays, and when I use this, ui seems to always be empty

function dupChar(char, num)
  ret = ''
  for ii=1,num do
    ret = ret..char
  end
  return ret
end

function drawLoad()
    --Pattern:
    -- X, Y, Text, FG, BG, Disabled for op, button action
    ui = {}
        ui.hungerStart = {3,4,'[',nil,nil,false,'none'}
        ui.hungerMid1 = {ui.hungerStart[1] + 1,ui.hungerStart[2],dupChar('-',20),nil,nil,false,'none'}
        ui.hungerMid2 = {ui.hungerStart[1] + 1,ui.hungerStart[2],hunger,nil,nil,false,'none'}
        ui.hungerStop = {ui.hungerStart[1] + 21,ui.hungerStart[2],']',nil,nil,false,'none'}

        ui.healthStart = {3,6,'[',nil,nil,false,'kill'}
        ui.healthMid1 = {ui.healthStart[1] + 1,ui.healthStart[2],dupChar('-',20),nil,nil,false,'health'}
        ui.healthMid2 = {ui.healthStart[1] + 1,ui.healthStart[2],health,nil,nil,false,'health'}
        ui.healthStop = {ui.healthStart[1] + 21, ui.healthStart[2],']',nil,nil,false,'heal'}
end

function drawAdv(pName, page)
    isOp = false
    pHealth = 10
    pHunger = 10
    health = dupChar('$',pHealth)
    hunger = dupChar('@',pHunger)
    drawLoad()
    color(custCol.default[1], custCol.default[2])
    stat.clear()
    term.clear()
    for index, value in ipairs(ui) do
        x, y, text, fg, bg, disOp, action = value[1],value[2],value[3],value[3],value[4],value[5],value[6]
        color(custCol.default[1], custCol.default[2])
        cur(x,y)
        if disOp then
            color(custCol.disOp[1], custCol.disOp[2])
        else
            color(fg,bg)
        end
        awrite(text)
    end
end

The other thing is to know if

for index, x, y, text, fg, bg, disOp, action in ipairs(ui) do

or

for index, value in ipairs(ui) do
        x, y, text, fg, bg, disOp, action = value[1],value[2],value[3],value[3],value[4],value[5],value[6]

is the way to get the list's entries

NB: the code is a bit messy, but it's because i tried to make it possible to use without using it in ComputerCraft as much as possible (some functions are still here and will not work with lua though)

Thank you for reading and maybe helping me :)

Ryan Stein
  • 7,930
  • 3
  • 24
  • 38
LazyShpee
  • 82
  • 12
  • The second method is correct to loop through a `lua-table`. Also, you're using `value[3]` twice. – hjpotter92 Mar 04 '13 at 21:17
  • Ok, thank you for that, and using value[3] was a mistake when I copied that part. – LazyShpee Mar 04 '13 at 21:50
  • I can't believe I was so stupid >< I tried to list by keys using ipairs(table) where I should have put pairs(table) Question answered, thank you guys, I'll edit my post when I get to use my computer. – LazyShpee Mar 04 '13 at 22:54

1 Answers1

1
for index, value in ipairs(ui) do
   local x, y, text, fg, bg, disOp, action = unpack(value)
   -- do something
end

function dupChar(char, num)
   return char:rep(num)
end
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64