I made a function to generate a menu once but can't remember where I put it, let me try again, it is very useful as you can make multiple different menus by just making a table of the choices and calling the function
function generatemenu(choices, results, spacing)
spacing=tonumber(spacing) or 7 - the horozontal spacing between selections
if type(choices)~="table" or type(results)~="table" then
print("ERROR GENERATING MENUnINPUT INCORRECT")
end
local selectedx=1
local selectedy=1
while true do
for y=1,#choices do --to print out the menu items and select the correct one
for x=1,#choices[y] do
if x==selectedx and y==selectedy then
term.setCursorPos(x*spacing-spacing,y)
write(">"..choices[y][x].."<")
else
term.setCursorPos(x*spacing-spacing+1,y) end
write(choices[y][x])
end
end
end
while true do
local event,param1=os.pullevent("key")
if param1==203 and selectedx>1 then selectedx=selectedx-1 break
elseif param1==205 and selectedx<#choices[selectedy] then selectedx=selectedx+1 break
elseif param1==200 and selectedy>1 then selectedy=selectedy-1 break
elseif param1==208 and selectedy<#choices then selectedy=selectedy+1 break
elseif param1==28 then return loadstring(results[y][x])()
end
end
if selectedx>#choices[y] then selectedx=#choices[y] end -- in case when you move down you are moving into a shorter row, this will put your selection at the end of that row
end
end
you call the function with 2 or 3 parameters, the first and second are tables in the following format
choices={}
for i=1,5 do --add rows
choices[i]={}
for a=1,5 do --add columns to each row
choices[i][a]="the choice that will show at this position (a,i) in your menu"
end
end
so each value in choices is a table, each one of those tables is a row and each value in those tables is a column in that row, essentially we are making a co-ordinate based menu in the format choices[y][x]
the 'results' table is the same as the choices in how it is structured but it is a table of functions to call for each menu option if it is selected, the values in this table must be surrounded by quotes as we are using loadstring to call them, if we did not you would just make the value the function name and do not include brackets after it, the problem with this is that you cannot put multiple functions in it (so we use loadstring)
here is an example of its use, a one line menu
mytable={}
mytable[1]={} --row one
mytable[1][1]="choice1" --first choice
mytable[1][2]="choice2" --second choice
mytable[1][3]="choice3" --third choice
myresults={}
myresults[1]={}
myresults[1][1]="term.clear() term.setCursorPos(1,1) print('you selected the first choice')"
myresults[1][2]="term.clear() term.setCursorPos(1,1) print('you selected the second choice')"
myresults[1][3]="term.clear() term.setCursorPos(1,1) print('you selected the third choice')"
generatemenu(mytable,myresults,10)