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 :)

3
  • The second method is correct to loop through a lua-table. Also, you're using value[3] twice. Commented Mar 4, 2013 at 21:17
  • Ok, thank you for that, and using value[3] was a mistake when I copied that part. Commented Mar 4, 2013 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. Commented Mar 4, 2013 at 22:54

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that is for part 2 and I still have a lot to learn in Lua ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.