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

Menus Help

Started by LukeBugg, 12 October 2012 - 12:18 PM
LukeBugg #1
Posted 12 October 2012 - 02:18 PM
Hello, I'm attempting to create a program for a tekkit train station. The program I am currently working on is to buy tickets. I have written a basic program using numerical inputs to designate where you want to go.

Its been tested and works fine but i want something a bit more advanced with a menu. I have watched a few tutorials and have a little understanding, but I just cant seem to get it to work.

I was just wondering if anyone can give me a few tips or hints on how to write the program I would like.

The basic program I used is;


shell.run("clear")
term.setCursorPos(1,1)
print("Welcome to the Rim Station")
print("")
print(" Ticket Sales")
print(" Select Ticket")
print(" ")
print("[1] Destination 1")
print("[2] Destination 2")
print("[3] Destination 3")
print("[4] Destination 4")

input = read()

A = "1"
B = "2"
C = "3"
D = "4"
E = "5"
F = "6"
G = "Edit"

if input == A then
shell.run("clear")
term.setCursorPos(1,1)
rs.setBundledOutput("back", 1)
print("Please Insert 1IC into payment shoot")
sleep(4)
print("")
print("Printing, Please Wait")
sleep(27)
shell.run("startup")

elseif input == B then
shell.run("clear")
term.setCursorPos(1,1)
rs.setBundledOutput("back", 16384)
print("Please Insert 1IC into payment shoot")
sleep(4)
print("")
print("Printing, Please Wait")
sleep(27)
shell.run("startup")


elseif input == C then
shell.run("clear")
term.setCursorPos(1,1)
rs.setBundledOutput("back", 64)
print("Please Insert 1IC into payment shoot")
sleep(4)
print("")
print("Printing, Please Wait")
sleep(27)
shell.run("startup")


elseif input == D then
shell.run("clear")
term.setCursorPos(1,1)
rs.setBundledOutput("back", 2048)
print("Please Insert 1IC into payment shoot")
sleep(4)
print("")
print("Printing, Please Wait")
sleep(27)
shell.run("startup")


elseif input == G then
shell.run("click")

else
shell.run("controls")

end

Thanks
Ditto8353 #2
Posted 12 October 2012 - 03:22 PM
Here are a couple little tricks that may help you:


--Make a function for each menu option. start() help() quit()
--These functions cannot take any parameters.
--Assign them to a table similar to this:
menuItems = {[0] = start,help,quit}
--The '[0] =' is both important AND optional depending on your personal preference.
--It starts the indexing at '0' instead of '1'.
--You don't need this, but without it you will require a few extra '+ 1's in some formulas.

--Initialize the position in the menu
local selected = 0
while true do
 local evt = {os.pullEvent()}   --The { } packs return values into a table. you can use ' local evt,k = os.pullEvent()' instead.
 if evt[1] == "key" then
  if evt[2] == UPARROW then   --I don't remember the Key Code for the UPARROW
   selected = (selected - 1) % (#menuItems + 1)   --The '#' gives the number of items in the table. the Modular division keeps us from going out of bounds.
  elseif evt[2] == DOWNARROW then
   selected = (selected + 1) % (#menuItems + 1)
  elseif evt[2] == ENTERKEY
   menuItems[selected]()   --This will call the function associated with the selected item.
  end
 end
end

…I hope I didn't make any errors in the code above…
ChaddJackson12 #3
Posted 13 October 2012 - 01:35 AM
Another thing that you can do, that would probably be neater (My opinion) is use os.pullEvent("key"). What this does, is wait for an input to do something; an example.

repeat -- Repeat just incase of an invalid key; like of someone presses key "A" then, have it do nothing. (That would exit the program.)
   event, key = os.pullEvent("key")
   os.sleep(0.1)
until key == 2 or key == 3 or key == 4 or key == 5 or key == 6 -- Know that this is the key's ID. Not the key itself. See the link for the ID's below.
if key == 2 then
   -- If someone presses 1.
elseif key == 3
   -- If someone presses 2.
elseif key == 4
   -- If someone presses 3.
elseif key == 5.
   -- If someone presses 4.
elseif key == 6
   -- If someone presses 5.
end
Find the key IDs here.

I hope that this helps!

By the way, os.pullEvent() can do many more things, so you may want to check that out on the wiki.