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

couple questions about menus

Started by subzero22, 03 September 2014 - 10:43 AM
subzero22 #1
Posted 03 September 2014 - 12:43 PM
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?
Zudo #2
Posted 03 September 2014 - 01: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.