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

Button Toggling Redstone Color On But Not Off?

Started by pilotstev, 08 November 2015 - 05:58 PM
pilotstev #1
Posted 08 November 2015 - 06:58 PM
I am working on a computer program that has an assortment of buttons that toggle on and off and I have been trying to get the state of the button to affect a certain color of a bundled cable.

This is what I have right now:


os.loadAPI("button")
m = peripheral.wrap("top")
m.clear()
function fillTable()
    button.setTable("Test", Test, 2, 10, 2, 2, colors.red)
    button.setTable("Test2", Test2, 2, 10, 4, 4, colors.yellow)
    button.screen()
end
function getClick()
    event,side,x,y = os.pullEvent("monitor_touch")
    button.checkxy(x,y)
end
redstone.setBundledOutput("right", 0)
function setBundledColor(side, color, state)
    if state then
	    if not colors.test(redstone.getBundledOutput(side), color) then
		    redstone.setBundledOutput(side, colors.combine(redstones.getBundledOutput(side), color))
	    end
    else
	    if colors.test(redstones.getBundledOutput(side), color) then
		    redstone.setBundledOutput(side, colors.subtract(redstone.getBundledOutput(side), color))
	    end
    end
end				   
function toggleBundledColor(side, color)
    redstone.setBundledOutput(side, (colors.test(redstone.getBundledOutput(side), color) and colors.subtract(redstone.getBundledOutput(side), color) or colors.combine(redstone.getBundledOutput(side), color)))
end

function Test()
    button.toggleButton("Test")
    toggleBundledColor("right", colors.white)
    print("Test")
end
function Test2()
    button.toggleButton("Test2")
    toggleBundledColor("right", colors.yellow)
    print("Test2")
end
fillTable()
while true do
    getClick()
end

My issue is, as it stands, it will toggle the state of its respective color on, but not off. I have followed a number of posts about this but I cant seem to find my error.

Special thanks to Lyqyd for all of his coding prowess, hes gotten me this far without a whole lot of stress thanks to his posts.

Also, obligatory "y u use direwolf button API m8"


Thank you to any help you can lend me. Its wracking my brain.
Bomb Bloke #2
Posted 08 November 2015 - 11:57 PM
CC 1.74 has an error within the bit API which is causing colours.subtract() to fail. Sticking something like this up the top of your script is one workaround:

function colours.subtract(cols, ...)
        for i = 1, #arg do
                if bit.band(cols, arg[i]) == arg[i] then
                        cols = cols - arg[i]
                end
        end

        return cols
end
colors.subtract = colours.subtract

Also, rs.testBundledInput() could be used to reduce things somewhat:

rs.setBundledOutput(side, rs.testBundledInput(side, color) and colors.subtract(rs.getBundledOutput(side), color) or colors.combine(rs.getBundledOutput(side), color))

Though really it may just be better to cache the output:

local curOut = rs.getBundledOutput(side)
rs.setBundledOutput(side, colors.test(curOut, color) and colors.subtract(curOut, color) or colors.combine(curOut, color))
pilotstev #3
Posted 10 November 2015 - 01:05 AM
Thank you so much, that's working perfectly.