Question

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

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top