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

Creating A Menu From A Table.

Started by NullSchritt, 10 August 2013 - 07:44 AM
NullSchritt #1
Posted 10 August 2013 - 09:44 AM
Hello all, was just wondering how to go about making a selectable menu from a table of data?

My main issue is that I don't 100% understand how to work with tables, as always, thanks for your time!
GamerNebulae #2
Posted 10 August 2013 - 10:14 AM
Selectable menu as in what? Buttons on a monitor, clicking in a Computer screen, can you clarify it maybe a bit. What I find to be a very clear tutorial about tables is this website: http://lua-users.org/wiki/TablesTutorial

I'm willing to help as well as long as I know what to do.
svdragster #3
Posted 10 August 2013 - 10:31 AM
There are also alot of tutorials about tables on the forums.
NullSchritt #4
Posted 10 August 2013 - 11:43 AM
Selectable menu as in what? Buttons on a monitor, clicking in a Computer screen, can you clarify it maybe a bit. What I find to be a very clear tutorial about tables is this website: http://lua-users.org.../TablesTutorial

I'm willing to help as well as long as I know what to do.

Just a simple console menu, the table is a list of destinations, I want to print each locations on the screen in a list, where they can use the directional keys to select an option and enter to select it.
Also thanks for the link but I still dont understand how to get the max elements in a table, or how to make them into a selectable menu, I understand how to print them but not how to allow them to be selected.
svdragster #5
Posted 10 August 2013 - 02:27 PM
To get the amount of elements in a table just use a #.
E.g.

numeros = {"uno", "dos", "tres"}
print(#numeros)
> 3

For a selectable menu you really should take a look at the tutorial section.
thomasbogue #6
Posted 10 August 2013 - 02:28 PM
It might be easier to label each choice with a number, and allow the user to press that number. Assuming you won't have more than 9-10 items per choice. In that case, you can do something like

for key,destination in pairs(locations) do
  print(key..") "..destination)
end
choicenum=tonumber(read())
choice=locations[choicenum]

Of course, you should probably have some checking to see if the user typed in a valid response.

I've actually writen an API (widget API) which would make this easy to do with the mouse. Using this API, it would be


chooser=widget.newChooser(defaultChoice,locations)
chooser:formTab()
chooser.tab.run()
choice=chooser.label

It wouldn't be a menu per se, but it would allow the user to select from the options.

Once I have create thread permissions I'll create a thread for it, but until then you get download it at
http://pastebin.com/kN4vX8cd