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

Menu code help

Started by Immow, 01 January 2013 - 07:30 AM
Immow #1
Posted 01 January 2013 - 08:30 AM
Hello peeps,

I tried following a tutorial on Youtube for what I thought was a nice piece of code, sad part is that the author did not put a link to the source… My programming skills are in beginner stage but I get the general idea.

When I run the code it outputs nothing… normally there would be errors to go about… so please help out :)/>

The video in question.

--[[ Local Variables ]]--

local termWidth, termHeight = term.getSize()
local selectedItem = 1
local InMainMenu = true
local InLightsMenu = false

--[[ Menu Methods ]]--
function Choice1()
    term.clear()
    term.setCursorPos(1,1)
    term.write("test")
    sleep(3)
end

function Choice2()
    inLightsMenu = true
    selectedItem = 1
    while inLightsMenu do
        term.clear()
        term.setCursorPos(1,1)
        printMenu(lightsMenu)
    
        event, key = os.pullEvent("key")
        onKeypressed(key, lightsMenu)
    end
end

function LightsOn()
    rednet.send(45, "on")
    inLightsMenu = false
    selectedItem = 1
end

function LightsOff()
    rednet.send(45, "off")
    inLightsMenu = false
    selectedItem = 1
end
    
function Exit()
    inMainMenu = false
end

--[[ Menu Definitions ]]--

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

lightsMenu = {
     [1] = { text = "on", handler = LightsOn },
    [2] = { text = "off", handler = LightsOff }
}

--[[ Printing Methods ]]--

function printMenu(menu)
    for i=1, #menu do
        if i == selectedItem then
            print (">> "..menu[i].text)
        else
            print ("   "..menu[i].text)
        end
    end
end

--[[ Handler Methods ]]--

function onKeyPressed( key, menu )
    if key == keys.enter then
        onItemSelected(menu)
    elseif key == keys.up then
        if selectedItem > 1 then
            selectedItem = selectedItem -1
        end
    elseif key == keys.down then
        if selectedItem < #menu then
            selectedItem = selectedItem +1
        end
    end
end

function onItemSelected( menu )
    menu[selectedItem].handler()
end

--[[ Main Method ]]--

function main()
    while inMainMenu do
        term.clear()
        term.setCursorPos(1,1)
        printMenu(mainMenu)
        
        event, key = os.pullEvent("key")
        onKeyPressed(key, mainMenu)
    end
end

main()
zekesonxx #2
Posted 01 January 2013 - 08:36 AM


--[[ Local Variables ]]--

local termWidth, termHeight = term.getSize()
local selectedItem = 1
local inMainMenu = true
local inLightsMenu = false

You capitalized the 'in's, making it 'In'. Because when a variable is not defined it returns false, the while loop in main() never ran.

InMainMenu and inMainMenu are not the same thing.
Immow #3
Posted 01 January 2013 - 08:41 AM
/me slams head against table, Well it works now but there still is a bug in InLightsMenu, when I move down in the menu it gives an error!

But it displays something :D/>

error is: menu:25: attempt to call nill
Orwell #4
Posted 01 January 2013 - 08:48 AM
/me slams head against table, Well it works now but there still is a bug in InLightsMenu, when I move down in the menu it gives an error!

But it displays something :D/>

error is: menu:25: attempt to call nill
On line 25, change:

onKeypressed(key, lightsMenu)
to:

onKeyPressed(key, lightsMenu)
With a capital 'P', like the function declaration on line 72 says.
Immow #5
Posted 01 January 2013 - 08:54 AM
Yay thank you both! I think I need a spellings checker for my code :P/>
zekesonxx #6
Posted 01 January 2013 - 09:10 AM
This is how CamelCase works:
Say you have a command that activates the right modem.
It would be activateRightModem(). Each word is capitalized, and the first word is all lowercase.