Jump to content




couple questions about menus


1 reply to this topic

#1 subzero22

  • Members
  • 97 posts

Posted 03 September 2014 - 11:43 AM

I was looking at this persons menu code http://pastebin.com/tB5ncjZr and I was wondering how I could make it so that if there's more selections than what can fit on the screen how to make it scrollable?

Another one is once the selection is selected where would I put the code to run the option in this case would be a shell.run

The last question on my mind is. But will be after I get everything tweaked out is would it be possible to make it so after the default menus an option that would ask for user input to add other programs to the menu also. I'm guessing that it would have to get the new options from another file?

#2 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 03 September 2014 - 12:05 PM

I'm not an expert on GUIs, but I can help on the last two questions.

The functions Choice1(), Choice2() and Exit() run on a selection. This code:

mainMenu = {
[1] = { text = "Choice 1", handler = Choice1 },
[2] = { text = "Choice 2", handler = Choice2 },
[3] = { text = "Exit", handler = Exit }
}

defines the functions to run when an option is selected. So, if you wanted to do a shell.run when choice 1 is selected, change Choice1() to this:

function Choice1()
 shell.run("/path/to/program")
end

For your last question, you could have a file. However, it would be complicated to have custom code to handle the option. It would be easier to do a program to run. You could have code like this in the menu program:

Add options to the menu:
-- details
print("Please enter the text to show in the menu.")
local textToShow = read()
print("Please enter the path to the program you want to run.")
local pathToProgram = read()

-- adds the entry
mainMenu[#mainMenu+1] = {["text"] = textToShow, ["handler"] = function() shell.run(pathToProgram) end}

-- load the file
local save_handler = fs.open("/.menulist", "w")

-- convert the table to a string
local serialized = textutils.serialize(mainMenu)

-- save the file
save_handler.writeLine(serialized)
save_handler.close()

Load the file:
-- open the file
local load_handler = fs.open("/.menulist", "r")

-- load it
mainMenu = textutils.unserialize(load_handler.readLine())

I'm not sure that this will work flawlessly.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users