This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
HurricaneCoder's profile picture

Table Question

Started by HurricaneCoder, 09 December 2013 - 09:55 PM
HurricaneCoder #1
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
Bomb Bloke #2
Posted 09 December 2013 - 11:26 PM
First run:

t[2] = {text = "Engine 2", func = engine2}  -- "engine2" is currently nil, so "t[state]["func"]" is nil too.
.
.
.
function engine2()  -- "engine2" is now a function, but "t[state]["func"]" is still nil.
 ...
end
.
.
.
t[state]["func"]()  -- Attempt to call nil.

Because you didn't declare the function as local, it stays loaded in memory when the script ends. So on the second run:

t[2] = {text = "Engine 2", func = engine2}  -- t[state]["func"] now points to the "engine2" function.
.
.
.
function engine2()  -- "engine2" was already in memory, but gets redeclared here (no real effect).
 ...
end
.
.
.
t[state]["func"]()  -- Function can now be called.

In short, localise your functions, and make sure you declare them before you attempt to do anything else with them.
HurricaneCoder #3
Posted 09 December 2013 - 11:39 PM
First run:

t[2] = {text = "Engine 2", func = engine2}  -- "engine2" is currently nil, so "t[state]["func"]" is nil too.
.
.
.
function engine2()  -- "engine2" is now a function, but "t[state]["func"]" is still nil.
...
end
.
.
.
t[state]["func"]()  -- Attempt to call nil.

Because you didn't declare the function as local, it stays loaded in memory when the script ends. So on the second run:

t[2] = {text = "Engine 2", func = engine2}  -- t[state]["func"] now points to the "engine2" function.
.
.
.
function engine2()  -- "engine2" was already in memory, but gets redeclared here (no real effect).
...
end
.
.
.
t[state]["func"]()  -- Function can now be called.

In short, localise your functions, and make sure you declare them before you attempt to do anything else with them.



Thx So much and I accidentally click report you when I try to thanks you sorry. This is my fault.