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

[LUA] [PROGRAMMING] Menu Programme Half Works

Started by Sarcastic_Bannana, 13 November 2012 - 01:12 AM
Sarcastic_Bannana #1
Posted 13 November 2012 - 02:12 AM
Hi,

Im working on a menu system to control my nuclear reactor.
The idea is when you select [ OPEN DOOR ] the door opens and the option changes to [ CLOSE DOOR ].
First time this works fine but selecting [ CLOSE DOOR ] while making the door close does not revert back to [ OPEN DOOR ]
This suggests to me that while the variable doors is changing between false and true the menu is not refreshing?

I've tried everything I could think of but hopefully someone has an idea how to fix this.
While modified the menu code is not mine.
Unfortuntly I cant find who's it is :P/>/>

Thanks in advance :)/>/>
(Sorry it's a lot of code)

http://pastebin.com/xf38q9L6
remiX #2
Posted 13 November 2012 - 04:44 AM
You had quite a few many end's now and there - surprised that it didn't give any errors.

I tested it and it works perfectly (but I may have gotten rid of rednet.send or rs.setOutput() commands now and then - but that's easy to put back in :P/>/>)

Pastebin

Raw code:
Spoiler

local w,h = term.getSize()
local running = true
local doors = false
local lights = false
local cooling = false
local i = 1

function printCentred( y, s )
    local x = math.floor((w - string.len(s)) / 2)
    term.setCursorPos(x,y)
    term.clearLine()
    term.write( s )
end

local nOption = 1

-- Display menu
local function drawMenu()
    term.clear()
    term.setCursorPos(1,1)
    term.write( "REACTOR ACCESS TERMINAL" )

    term.setCursorPos(w-25,1)
    if nOption == 1 then
        if not doors then
            term.write("OPEN REACTOR DOOR")
        else
            term.write("CLOSE REACTOR DOOR")
        end
    elseif nOption == 2 then
        if not lights then
            term.write("TURN LIGHTS ON")
        else
            term.write("TURN LIGHTS OFF")
        end
    elseif nOption == 3 then
        if not cooling then
            term.write("TURN COOLING OFF")
        else
            term.write("TURN COOLING ON")
        end
    else
        term.write("Console")
    end
end

-- Display the frontend
term.clear()
local function drawFrontend()
    printCentred( math.floor(h/2) - 3, "" )
    printCentred( math.floor(h/2) - 2, "AVALIABLE COMMANDS" )
    printCentred( math.floor(h/2) - 1, "" )
    if doors == false then
        printCentred( math.floor(h/2) + 0, ((nOption == 1) and "[ OPEN REACTOR DOOR ]") or "OPEN REACTOR DOOR" )
    else
        printCentred( math.floor(h/2) + 0, ((nOption == 1) and "[ SEAL REACTOR DOOR ]") or "SEAL REACTOR DOOR" )
    end

    if lights == false then
        printCentred( math.floor(h/2) + 1, ((nOption == 2) and "[ TURN LIGHTS ON ]") or "TURN LIGHTS ON" )
    else
        printCentred( math.floor(h/2) + 1, ((nOption == 2) and "[ TURN LIGHTS OFF ]") or "TURN LIGHTS OFF" )
    end

    if cooling == false then
        printCentred( math.floor(h/2) + 2, ((nOption == 3) and "[ DEACTIVATE COOLING ]") or "DEACTIVATE COOLING" )
    else
        printCentred( math.floor(h/2) + 2, ((nOption == 3) and "[ ACTIVATE COOLING ]") or "ACTIVATE COOLING" )
    end
    
    printCentred( math.floor(h/2) + 3, ((nOption == 4) and "[ CONSOLE ]") or "CONSOLE" )
end

-- Call functions for display menu

while i == 1 do

    drawMenu()
    drawFrontend()

    while true do
        local e,p = os.pullEvent()
        if e == "key" then
            local key = p
            if key == 17 or key == 200 then
                -- Up
                if nOption > 1 then
                    nOption = nOption - 1
                    drawMenu()
                    drawFrontend()
                end
            elseif key == 31 or key == 208 then
                -- Down
                if nOption < 4 then -- Change 3 by the number of option.
                    nOption = nOption + 1
                    drawMenu()
                    drawFrontend()
                end
            elseif key == 28 then
                -- Enter
                break
            end
        end
    end

    -- Conditions
    if nOption == 1 then
        if doors then
            doors = false
            rednet.send(39,"rdo1")
        else
            doors = true
        end
    elseif nOption == 2 then
        if lights then
            lights = false
            --rednet.send(39,"l1")
        else
            lights = true
            --rednet.send(39,"l0")
        end
    elseif nOption == 3 then
        if cooling then
            cooling = false
            --rednet.send(39,"c0")
        else
            cooling = true
        end
        --rednet.send(39,"c1")
    elseif nOption == 4 then
        running = false
        term.clear()
        term.setCursorPos(1, 1)
        break
    end
end
Sarcastic_Bannana #3
Posted 13 November 2012 - 06:26 AM
Fantastic, It works perfectly!

Now I'm off to fix some other suspicious glitches in my system.

THANK YOU!!!! :P/>/>