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

[Lua] [Error] calling a function from a string

Started by erikvader, 17 March 2013 - 09:41 AM
erikvader #1
Posted 17 March 2013 - 10:41 AM
i'm making a menu program for my mystcraft stargate. this is how it should work:
i will select the age i want from the menu and press enter, a rednet signal will go to a turtle that will start the stargate that will lead to the desired destination.

but my problem is that it fails on line 63. Attempt to call string.
i tried to change "func1" to func1. but then it fails on the same line again but with the error message "attempt to call nil"

Thanks in advance!


rednet.open("top")
page = 1
curPos = 1
LR = "a"
menu = {
	   {{"age3", "func1"}, {"age4", "func2"}, {"age5"}, {"age6"}, {"age8"}, {"age9"}, {"age10"}},
	   {{"age11"}, {" "}, {" "}, {" "}, {" "}, {" "}, {" "}}
	}  
local function func1()
  term.setCursorPos(1, 1)
  term.write("yay!!!")
  sleep(5)
end
  
w, h = term.getSize()
local function printCentered(str, ypos)
  term.setCursorPos(w/2 - #str/2, ypos)
  term.write(str)
end
local function prtMenu(page)
  term.clear()
  printCentered("Stargate", 1)
  printCentered(string.rep("-", w), 2)
  for i = 1, 7 do
	printCentered(menu[page][i][1], i*2 + 2)
  end
  term.setCursorPos(4, 18)
  term.write("Left")
  term.setCursorPos(43, 18)
  term.write("Right")
end
local function prtSelector(selPos, LR, page)
  if selPos > 0 and selPos < 8 then
	printCentered(string.rep("-", #menu[page][selPos][1]), selPos*2 + 3)
  elseif selPos == 8 then
	if LR == "a" then
   term.setCursorPos(4, 19)
   term.write(string.rep("-", 4))
elseif LR == "b" then
   term.setCursorPos(43, 19)
   term.write(string.rep("-", 5))
end
  end
end
local function keyEnter()
  if curPos == 8 then
	if LR == "a" then
   if page > 1 then
	 page = page - 1
   end
elseif LR == "b" then
   if page < #menu then
	 page = page + 1
   end
	end
  elseif curPos > 0 and curPos < 8 then
	menu[page][curPos][2]()  --This is line 63
  end
end
local function navigate()
if key == 200 then
  if curPos > 1 then
	curPos = curPos - 1
  end
elseif key == 208 then
  if curPos < 8 then
	curPos = curPos + 1
  end
elseif key == 203 then
  LR = "a"
elseif key == 205 then
  LR = "b"
elseif key == 28 then
  keyEnter()
end
end

while true do
prtMenu(page)
prtSelector(curPos, LR, page)
event, key = os.pullEvent("key")
navigate()
end
Lyqyd #2
Posted 17 March 2013 - 12:11 PM
Split into new topic.

You should just use the function value directly. Put func1 above the table declaration, take the quotes out from around func1 in the table declaration, and you should be good to go.
erikvader #3
Posted 18 March 2013 - 07:45 AM
Thank you! :D/>