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

equals expected on term.setCursorPos(1,1)

Started by brownie4990, 03 August 2013 - 07:59 AM
brownie4990 #1
Posted 03 August 2013 - 09:59 AM
Title: equals expected on term.setCursorPos(1,1)

I am trying to write a function that generates a menu with arrow key option switching. I would like to make use of a table input where the value of each key is the option, due to this function being in scope of a larger project. When run this code returns the error: BIOS:337: [string "menu.lua"] 27: '=' expected. I am pretty sure that setCursorPos does not require any equals leading me to think there was a different error in the function causing it. Improvements tot he code is gladly accepted as long as they are explained since I am pretty new to lua as a programming language and would like to learn.


function generateMenu(options)
term.clear()
n = 1
while true do
  sleep(1)
  term.clear()
  m = n + 1
  print("Please select an option")
  for i=1, 1000000, 1 do
   if options[i] == nil then
	break
   else
	print(options[i])
   end
  end
  term.clearLine(m)
  term.setCursorPos(1,m)
  print("[ "..options[n].." ]")
  a, b= os.pullEventRaw()
  if a == "key" then
   if b==200 and n>1 then n=n-1 end
   if b==208 and n<=l then n=n+1 end
   if b==28 then break end
  end
end
term.clear
term.setCursorPos(1,1) -- line 27
return n
end

--testing the function
opt = {}
opt[1] = "first option"
opt[2] = "seccond option"
opt[3] = "third option"
result = generateMenu(opt)
print(result)
Bubba #2
Posted 03 August 2013 - 10:32 AM
Split into new topic.
DaGamer12345 #3
Posted 03 August 2013 - 11:19 AM
You forgot the parentheses on line 26 for term.clear [should be term.clear()]. It's thinking term.clear is a variable, so that's why it's expecting '=' (to assign a value).
brownie4990 #4
Posted 04 August 2013 - 08:33 AM
Thanks for your help that was a stupid mistake that I should have spotted :)/>
TheOddByte #5
Posted 04 August 2013 - 08:52 AM
And a tip for your for loop

#options --This gets the length of your table/string etc.
And instead of checking all numbers from 1-100000 or what you were using it would only check from 1 to the total table lenght.

Now you could do something like this

for i = 1,#options do -- From 1-<Table Length>
 print(options[i]) -- Would ofcourse print what's in the table
end