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

[Lua][Question]

Started by predatorxil, 24 August 2012 - 12:02 AM
predatorxil #1
Posted 24 August 2012 - 02:02 AM
I am trying to make a menu where you can scroll through useing the arrow keys, I have seen this before, and i understand that it might be easier to just use the number system by typing the number for waht choice you want, but i would like the ability to scroll.
If i didnt explain so well, srry, but please help by either posting links, code i can input or edit, or anyting.

And i wanted to add, im trying to make multiple users but not sure if i can use the elseif, if, or even the 'or' statement.


THanks so much!
Grim Reaper #2
Posted 24 August 2012 - 03:03 AM
Here's some code I wrote up for a little menu:

Spoiler

Selection = 1 -- The variable that holds the current selection that we're on.
bRunning = true -- The variable that holds whether or not the program is currently running.
function DrawSelections() -- Draws the selections and signals if they're selected or not.
if Selection == 1 then -- If the selection is on the first option.
  print( "[Option1]" ) -- Draw the selection with brackets around it as to signal that it is selected.
else -- If the selection is not on the first option.
  print( " Option1 " ) -- Draw the selection without braackets around it as to signal that it is not selected.
end

if Selection == 2 then -- If the selection is on the second option.
  print( "[Option2]" ) -- Draw the selection with brackets around it as to signal that it is selected.
else -- If the selection is not on the second option.
  print( " Option2 " ) -- Draw the selection without brackets around it as to signal that it is not selected.
end

if Selection == 3 then -- If the selection is on the last option.
  print( "[Exit]" ) -- Draw the selection with brackets around it as to signal that it is selected.
else -- If the selection is not selected.
  print( " Exit " ) -- Draw the selection without brackets around it as to signal that it is not selected.
end
end
function Clear() -- Clears the screen.
term.clear() -- Clear all of the current text from the screen.
term.setCursorPos( 1, 1 ) -- Set the cursor at the upper hand left corner at (1, 1).
end
function HandleKeyPress( Key ) -- Takes a key number and moves the selection accordingly.
if Key == 200 then -- If the up key is pressed.
  if Selection > 1 then -- If the selection is greater than 1; if there are more selections before the current one.
   Selection = Selection - 1 -- Move the selection up one on the menu.
  end
elseif Key == 208 then -- If the down key is pressed.
  if Selection < 3 then -- If the selectoin is less than 3; if there are more selections after the current one.
   Selection = Selection + 1 -- Move the selection down one on the menu.
  end
elseif Key == 28 then -- If the enter key is pressed.
  if Selection == 1 then -- If the selection is on the first option and the enter key is pressed.
   term.setCursorPos( 5, 16 ) -- Place the cursor at x = 5, y = 16 (5, 16)
   print( "Option1 pressed!" ) -- Print the text within the parenthesis.
   sleep( 2 ) -- Wait 2 seconds before executing the next instruction.
  elseif Selection == 2 then -- If the selection is on the second option and the enter key is pressed.
   term.setCursorPos( 5, 16 ) -- Place the cursor at x = 5, y = 16 (5, 16)
   print( "Option2 pressed!" ) -- Print the text within the parenthesis.
   sleep( 2 ) -- Wait 2 seconds before executing the next instruction.
  elseif Selection == 3 then -- If the selectoin is on the third or 'Exit' option and the enter key is pressed.
   -- Reset the screen to look like the computer just turned on.
   Clear() -- Clear the screen.
   print( os.version() ) -- Print the OS version.
  
   bRunning = false -- Terminate the main program loop.
  end
end
end
while bRunning do -- While the bRunning variable holds a value of true, or if is not boolean, holds a value other than nil.
Clear() -- Clear the screen so we may update it.
DrawSelections() -- Redraw the current state of the menu.

local event, key = os.pullEvent( "key" ) -- Wait for any key press.
HandleKeyPress( key ) -- Handle the key pressed by passing the key value from the event to the HandleKeyPress function.
end

There should be plenty of comments to guide you through everything that is going on in that script, but if you don't understand something feel free to PM me.

As for multiple users might I suggest that you read the user information from a file into a table?
If you need any help with that I'm sure the community here would be glad to help.

Hope I helped! :D/>/>
KaoS #3
Posted 24 August 2012 - 08:58 AM
check out this post http://96.31.75.244/forums2/index.php?/topic/3253-help-advanced-menu/ I made a menu generator