Posted 09 December 2013 - 10:55 PM
local t = {}
t[1] = {text = "Engine", func = engine1}
t[2] = {text = "Engine 2", func = engine2}
t[3] = {text = "Engine 3",func = engine3}
local w,h = term.getSize()
local state = 1
function draw()
term.clear()
for i = 1,#t do
if state == i then
term.setCursorPos(((w-string.len("--> "..t[1]["text"]))/2)+1,((h/2)-1)+i)
term.write("--> "..t[i].text)
elseif state ~= i then
term.setCursorPos((((w-string.len("--> "..t[1]["text"]))/2)+1),((h/2)-1)+i)
term.write(" "..t[i].text)
end
end
end
function draw2()
end
function engine2()
rs.setOutput("back",true)
end
draw()
while true do
local event,key = os.pullEvent("key")
if key == 200 then
if state > 1 then
state = state - 1
draw()
elseif state == 1 then
state = #t
draw()
end
elseif key == 208 then
if state < #t then
state = state + 1
draw()
elseif state == #t then
state = 1
draw()
end
elseif key == 28 then
t[state]["func"]()
end
end
why does defining the table on top give me an error when I run it the first time and the second time is fine?
If I do this
local t = {}
local w,h = term.getSize()
local state = 1
function draw()
term.clear()
for i = 1,#t do
if state == i then
term.setCursorPos(((w-string.len("--> "..t[1]["text"]))/2)+1,((h/2)-1)+i)
term.write("--> "..t[i].text)
elseif state ~= i then
term.setCursorPos((((w-string.len("--> "..t[1]["text"]))/2)+1),((h/2)-1)+i)
term.write(" "..t[i].text)
end
end
end
function draw2()
end
function engine2()
rs.setOutput("back",true)
end
draw()
t[1] = {text = "Engine", func = engine1}
t[2] = {text = "Engine 2", func = engine2}
t[3] = {text = "Engine 3",func = engine3}
while true do
local event,key = os.pullEvent("key")
if key == 200 then
if state > 1 then
state = state - 1
draw()
elseif state == 1 then
state = #t
draw()
end
elseif key == 208 then
if state < #t then
state = state + 1
draw()
elseif state == #t then
state = 1
draw()
end
elseif key == 28 then
t[state]["func"]()
end
end
Everything work fine on this