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

Why won't this work

Started by luckyluke206, 02 November 2012 - 03:38 AM
luckyluke206 #1
Posted 02 November 2012 - 04:38 AM
I am making a computer powered blaze farm. This code is supposed to create a GUI for controlling the farm. This is one of my first attempts at coding so I am confused at why this won't work. The menu doesn't update like i expected it to. Also, turning on and off the farm doesnt work from the menu. I spent many hours trying to figure out what was wrong, but i still couln't figure it out so now i am asking you.

local cursorPos = 0 –var used to keep track of cursor
local status –whether or not farm is on
local waterFlow –whether or not water is flowing into chamber

–toggles water in blaze chamber to kill blazes or push drops
function switchWater()
rs.setBundledOutput("back", 16)
sleep(1)
rs.setBundledOutput("back", 0)
end

–crushes blazes to bring them down to 1 heart
function crush()
rs.setBundledOutput("back", 4)
sleep(1.5)
rs.setBundledOutput("back", 5)
sleep(9.5)
rs.setBundledOutput("back", 4)
sleep(10)
rs.setBundledOutput("back", 0)
end

–raises spawner with frames to stop spawning
function raiseSpawner()
for i = 1, 3 do
rs.setBundledOutput("back", 2)
sleep(.4)
rs.setBundledOutput("back", 0)
sleep(.4)
end
end

–lowers spawner with frames to begin spawning
function lowerSpawner()
for i = 1, 3 do
rs.setBundledOutput("back", 8)
sleep(.4)
rs.setBundledOutput("back", 0)
sleep(.4)
end
end

–function that is executed when i press enter on menu
function enter()
if cursorPos == 0 and status == "off" then
lowerSpawner()
elseif cursorPos == 0 and status == "on" then
raiseSpawner()
elseif cursorPos == 1 then
switchWater()
elseif cursorPos == 2 then
crush()
elseif cursorPos == 3 then
os.shutdown()
end
end

–checks if farm and water is on to relay to the menu
function checkStatus()
if rs.getBundledInput("bottom", 1 or 3) == false then
status = "On"
elseif rs.getBundledInput("bottom", 0 or 2) then
status = "Off"
end
–when you turn on/off the farm, it activates an rsnor latch
–which goes into bottom of the computer as a white line(RP2)
if rs.getBundledInput("bottom", 1 or 3) == false then
waterFlow = "On"
elseif rs.getBundledInput("bottom", 0 or 2) then
waterFlow = "Off"
–the line of redstone going to pistons to control water feed –into bottom of computer as an orange line
end
end

– write status on the menu
function writeStatus()
checkStatus()
term.clear()
term.setCursorPos(1, 1)
print("Blaze farm control")
term.setCursorPos(4, 4)
print("Blaze Farm Status: ".. status)
term.setCursorPos(4, 6)
print("Water Flow: ".. waterFlow)
term.setCursorPos(4, 8)
print("Crush!")
term.setCursorPos(4, 10)
print("Exit")
term.setCursorPos(3, (cursorPos + 2) * 2)
print(">")
–term.setCursorPos(4, 13)
–print(cursorPos)
end

writeStatus()

while true do
event, param1 = os.pullEvent()
if event == "key" and param1 == 200 then
cursorPos = cursorPos - 1
writeStatus()
elseif event == "key" and param1 == 208 then
cursorPos = cursorPos + 1
writeStatus()
elseif event == "key" and param1 == 28 then
enter()
writeStatus()
end
end
remiX #2
Posted 02 November 2012 - 04:51 AM
This is a menu function that I use and it works great.

Spoiler

local function menu(...)
    local sel = 1
    local list = {...}
    local offX,offY = term.getCursorPos()
    local curX,curY = term.getCursorPos()
    while true do
        if sel > #list then sel = 1 end
        if sel < 1 then sel = #list end
        for i = 1,#list do
            term.setCursorPos(offX,offY+i-1)
            if sel == i then
                print("[ "..list[i].." ]")
            else
                print("- "..list[i].."  ")
            end
        end
        while true do
            local e,e1 = os.pullEvent()
            if e == "key" then
                if e1 == 200 then -- up key
                    sel = sel-1
                    break
                end
                if e1 == 208 then -- down key
                    sel = sel+1
                    break
                end
                if e1 == 28 then -- enter key
                    term.setCursorPos(curX,curY)
                    return list[sel],sel
                end
            end
        end
    end
end

Thats the function for how it works, to have what text that must display on the screen do this:

Spoiler

local selection = menu("Copy","Label","Clear data","List files","Eject","Refresh")
if selection == "Refresh" then return lol()
elseif selection == "Copy" then copy()
elseif selection == "Label" then setLabel()
elseif selection == "List files" then term.clear() listFiles()
elseif selection == "Eject" then term.clear() diskEject()
elseif selection == "Clear data" then term.clear() clearData() end

Works beautifully.
flighteur #3
Posted 02 November 2012 - 05:42 AM
Or follow this tutoriel : http://www.youtube.com/watch?v=qzW3pxwW_iM&amp;list=PL69E38751E0C625FE&amp;index=17&amp;feature=plpp_video