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

bios:14: [string "menu"]:5: unexpected symbol

Started by STaSHZILLA420, 08 March 2016 - 07:13 PM
STaSHZILLA420 #1
Posted 08 March 2016 - 08:13 PM
Hello. This is my first post and I am fairly new to lua so pardon me if this fix is quite easy.

I was trying to create a menu for computercraft so i could use it in later programs. Like most newcomers, I took to tutorials for help. I basically created a menu with 3 options: option 1, option 2 and an exit. I have a good structure of the code, but when I try to run it, I get an error (title)

Here is my code:
--[[Local Variables]]--

local termWidth, termHeight = term.getSize()
local selectedItem = 1
local running true


--[[Menu Methods]]--

function choice1()

  term.clear()
  term.setCursorPos(1, 1)
  term.write("Hello, my name is "..os.getComputerLabel())
  sleep(3)

end

function choice2()

  term.clear()
  term.setCursorPos(1, 1)
  term.write("This is just a test.")
  sleep(3)

end

function Exit()
  running = false
end


--[[Menu Definitions]]--

mainMenu{
  [1] = {text = "Choice 1", handler = choice1 },
  [2] = {text = "Choice 2", handler = choice2 },
  [3] = {text = "Exit", handler = Exit }

}

--[[Printing Methods]]--

function printMenu( menu )
  for i=1,#menu do
	if i == selectedItem then
	  print(">> " ..menu[i].text)
	else
	  print("   " ..menu[i].text)
   end
end
end

--[[Handler Methods]]--

function onKeyPressed(key, menu)
  if key == keys.enter then
	onItemSelected( menu )
  elseif key == keys.up then
	if selectedItem > 1 then
	  selectedItem = selectedItem - 1
	end
  elseif key == keys.down then
	if selectedItem < #menu then
	  selectedItem = selectedItem + 1
	end
  end
end

function onItemSelected( menu )
  menu[selectedItem].handler()
end


--[[Main Method]]--

function main()
  while running do
	term.clear()
	term.setCursorPos(1, 1)
	printMenu(mainMenu)

	event, key = os.pullEvent("key")
	onKeyPressed(key, mainMenu)
  end
end

Thank you in advance to whoever helps me. Hopefully i've provided enough information for the solution.
Lyqyd #2
Posted 08 March 2016 - 10:10 PM
You forgot the assignment operator (=) when you assign true to the running variable.
STaSHZILLA420 #3
Posted 08 March 2016 - 10:20 PM
Yes, I've tried that however, I end up getting
".temp:30: attempt to call nil"

Side note: I also noticed that I forgot to call main() at the bottom so i fixed that

Thanks for the quick response.
Bomb Bloke #4
Posted 08 March 2016 - 11:26 PM
In case you haven't spotted it, the numbers within the errors are pointing you to the line number where the fault lies. You get an error on the first line to contain one, so fixing your fault on the fifth line reveals the flaw on the thirtieth.

That said, the thirtieth line of what you've shown us isn't attempting to call any functions, so it seems you've messed with the spacing somewhat since posting it. Check your current line 30 for mis-typed function names. If the spelling looks right, think about whether the target exists at the time when you attempt to call it, and if you're still stuck, post your current code.
STaSHZILLA420 #5
Posted 09 March 2016 - 12:10 AM
line 30 is the start of my "mainMenu" table

the code i posted is the code im using. It hasnt been touched or altered with the exception of me calling main() at the bottom of the code. I mentioned that in my 2nd post. Taking out main() still shows same error.
Bomb Bloke #6
Posted 09 March 2016 - 12:16 AM
line 30 is the start of my "mainMenu" table

the code i posted is the code im using.

The code you posted puts the content of line 30 as "end". Your mainMenu definition is the thirty-fifth line there.

But in regards to that line, you're missing an equals symbol, so the Lua VM thinks you're trying to call mainMenu with the table you really want to assign to mainMenu.
Edited on 08 March 2016 - 11:16 PM
STaSHZILLA420 #7
Posted 09 March 2016 - 01:01 AM
The code you posted puts the content of line 30 as "end". Your mainMenu definition is the thirty-fifth line there.

But in regards to that line, you're missing an equals symbol, so the Lua VM thinks you're trying to call mainMenu with the table you really want to assign to mainMenu.

Thank you! Solved! Ok, so it appears there was a spacing difference with my code and the code posted. with that being said, line 30 in-game was my table. I simple did what you said it was right! I appreciate your quick response and you patience with my somewhat noobish question. You are a life saver. And thanks again lyqyd for the first half of the solution.