Well its actually quite simple really. Heres an example of the code you would need to do this sort of thing without having an auto detect of how big each string is and automatically center it on the page.
function menu(table) -- Creating a function to run the code. (table) is something that is added when calling the function, see below.
local sel = 1 -- Setting a variable for the selection.
clear() -- This is just an API function I use on my computers, saves a bit of time when trying to rip out a code fast.
while true do
term.setCursorPos(1, 1) -- This can be changed to anything so you can move it anywhere.
for i = 1,#table do -- Telling the program to run this part of the code to write [ ] around table[i] so if i = 2 then it would print the second
-- string in the table with [ ] around it but nothing else
if i == sel then write"[" end
write(table[i])
if i == sel then write"]" end
print("")
end -- Ending the 'for' loop
local r,s = os.pullEvent() -- Detecting any events that may happen.
if r == "key" then -- Refining the events detected to only key strokes.
if s == 200 then sel = sel - 1 end -- If the up key is pushed take 1 off of 'sel' variable.
if sel == 0 then sel = #table end -- still checking through whats happening to the table, so if 'sel' is 0 as in you have used the up key
-- when on the number 1 table item it will go to the last item in the table
if s == 208 then sel = sel + 1 end -- When the down key is pushed add 1 to 'sel'.
if sel == #table +1 then sel = 1 end -- Pretty much the same as 'if sel == 0 then' but going in reverse.
if s == 28 then -- If the 'enter' key is pushed.
clear() -- Just clearing the screen.
return table[sel] -- Telling the function to return whatever item in the table is selected to the rest of the program.
end -- Ending the 'enter' key if statement.
end -- Ending the os.pullEvent() 'key' if statement.
end -- Ending the while loop.
end -- Closing the function.
As for using this function to utilise the menu.
Basically we need to call upon the function, to do this first of all we need a table.
A table can be created by using { }'s with strings separated by comma's.
Next we need to call the table a variable so we can call on the function nice and cleanly.
Finally you call the function using the variable.
This is what it would look like.
items = {"String 1", "String 2", "String 3"} --[[ You can call 'items' whatever you like as it is a variable, it just has to match up to the corresponding
variable in the function above, so if the function was; function menu(table, Ylocation, Xlocation) then
you wouldn't go and put 'items' in the 2nd or 3rd place inside the ( )'s when calling the function.]]
menu(items) -- Calling the function and telling it to use the table 'items'
Now how to make it so that when you press enter it runs another program or script.
This is really easy. Heres an example of how you would do it;
items = {"String 1", "String 2", "String 3"}
result = menu(items) -- The reason this is now a variable is so that when the function returns the selected menu items you have something to use in an if statement
if result == "String 1" then
--[Put your code here for what you want this option to run]
end
if result == "String 2" then
--[Put your code here for what you want this option to run]
end
if result == "String 3" then
--[Put your code here for what you want this option to run]
end
I'm not 100% sure if the above example would work, haven't tested it as usually I just use elseif to branch off the different results which results in messy code.
You can also call your menu (If in a function) when another menu has returned a certain item. So say you selected "String 1" and pushed enter, you could
then make a if statement to say; if result == "String 1" then menu(items2). Or something like that so it runs the menu again with other items. Its all up to
you.
Anyway, I hope this is of some use to you and helps you figure out a menu of your own. You are free to use the menu code above as long as you give me
credit for it, you may also edit it as long as you give me credit for the original bits of the code.
Please tell me if anything in this post is confusing you or is incorrect as well. I havent tested some of it.
Welcome to Lua my friend =D