Jump to content




Creating A Menu From A Table.


  • You cannot reply to this topic
5 replies to this topic

#1 NullSchritt

  • Members
  • 43 posts

Posted 10 August 2013 - 08: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!

#2 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 10 August 2013 - 09: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.../TablesTutorial

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

#3 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 10 August 2013 - 09:31 AM

There are also alot of tutorials about tables on the forums.

#4 NullSchritt

  • Members
  • 43 posts

Posted 10 August 2013 - 10:43 AM

View PostGamerNebulae, on 10 August 2013 - 09:14 AM, said:

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.

#5 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 10 August 2013 - 01: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.

#6 thomasbogue

  • Members
  • 6 posts

Posted 10 August 2013 - 01: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





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users