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

Need a help with a touchscreen button program :)

Started by KiraGio, 14 December 2015 - 01:09 PM
KiraGio #1
Posted 14 December 2015 - 02:09 PM
Hi everyone :)/>

I'm pretty noobish on Lua programming but I was trying to make a monitor with two buttons that would toggle on/off, activating a bundled cable.

I'm using the Touchpoing API by Lyqyd and this is (roughly) the program:


os.loadAPI("touchpoint")


local t = touchpoint.new("top")

function spawner()
		t:toggleButton("Spawner")
		if rs.getBundledOutput("back", colors.white) == true then
				rs.setBundledOutput("back", colors.white, false)
		else
				rs.setBundledOutput("back", colors.white, true)
		end
end

function finestra()
		t:toggleButton("Finestra")
		if rs.getBundledOutput("back",colors.red) == true then
				rs.setBundleOutput("back", colors.red, false)
		else
				rs.setBundledOutput("back",colors.red, true)
		end
end

tasti = function()
		t:add("Spawner", spawner, 2, 2, 14, 4, colors.red, colors.lime)
		t:add("Finestra",  finestra, 16, 2, 28, 4, colors.red, colors.lime)
		t:draw()
end

tasti()
t:run()

Everything working fine until I try to toggle off a button: the wire won't turn off.
It turns off only when the other button is pressed, which is pretty confusing to me.. any ideas? :)/>

Thanks~



Bomb Bloke #2
Posted 14 December 2015 - 02:40 PM
rs.setBundledOutput() accepts two parameters; you're passing in three, so it ignores the third.

The idea is that if you specify that a colour, then it'll simply turn that colour on. Don't specify the colour and it turns it off. To manage combined colour values such as those used in bundled cables, we use the functions from the colours API:

                if colours.test(rs.getBundledOutput("back"), colours.red) then
                                rs.setBundledOutput("back", colours.subtract(rs.getBundledOutput("back"), colours.red)
                else
                                rs.setBundledOutput("back", colours.combine(rs.getBundledOutput("back"), colours.red)
                end
KiraGio #3
Posted 14 December 2015 - 03:03 PM
Well, I got the general idea, so thanks for that.

But.. what am I doing wrong?


os.loadAPI("touchpoint")

local t = touchpoint.new("top")
function spawner()
	    t:toggleButton("Spawner")
	    if colors.test(rs.getBundledOutput("back"), colors.red) then
		   rs.setBundledOutput("back", colors.subtract(rs.getBundledOutput("back"), colors.red))
	    else
		   rs.setBundledOutput("back", colors.combine(rs.getBundledOutput("back"), colors.red))
	    end
end
function finestra()
	    t:toggleButton("Finestra")
	    if colors.test(rs.getBundledOutput("back"), colors.white) then
			  rs.setBundledOutput("back", colors.subtract(rs.getBundledOutput("back"), colors.white))
	    else
			  rs.setBundledOutput("back", colors.combine(rs.getBundledOutput("back"), colors.white))
	    end
end	   
tasti = function()
	    t:add("Spawner", spawner, 2, 2, 14, 4, colors.red, colors.lime)
	    t:add("Finestra",  finestra, 16, 2, 28, 4, colors.red, colors.lime)
	    t:draw()
end
tasti()
t:run()

Thank you :)/>
KiraGio #4
Posted 14 December 2015 - 04:19 PM
EDIT: Updated grammar
Lyqyd #5
Posted 14 December 2015 - 05:12 PM
If you're using ComputerCraft 1.74, you're experiencing the problem with the broken bit function causing colors.subtract to not actually remove the color specified. Bomb Bloke had a replacement function floating around, or you could update to CC 1.75 to fix that issue.
Bomb Bloke #6
Posted 15 December 2015 - 12:59 AM
If it is that, then this sort of block:

            if colors.test(rs.getBundledOutput("back"), colors.white) then
                          rs.setBundledOutput("back", colors.subtract(rs.getBundledOutput("back"), colors.white))
            else
                          rs.setBundledOutput("back", colors.combine(rs.getBundledOutput("back"), colors.white))
            end

… can simply be re-written as:

            if colors.test(rs.getBundledOutput("back"), colors.white) then
                          rs.setBundledOutput("back", rs.getBundledOutput("back") - colors.white)
            else
                          rs.setBundledOutput("back", rs.getBundledOutput("back") + colors.white)
            end

Otherwise, your main problem is that you're not explaining what's happening when you run your code! :P/>
KiraGio #7
Posted 15 December 2015 - 03:54 PM
If it is that, then this sort of block:

			if colors.test(rs.getBundledOutput("back"), colors.white) then
						  rs.setBundledOutput("back", colors.subtract(rs.getBundledOutput("back"), colors.white))
			else
						  rs.setBundledOutput("back", colors.combine(rs.getBundledOutput("back"), colors.white))
			end

… can simply be re-written as:

			if colors.test(rs.getBundledOutput("back"), colors.white) then
						  rs.setBundledOutput("back", rs.getBundledOutput("back") - colors.white)
			else
						  rs.setBundledOutput("back", rs.getBundledOutput("back") + colors.white)
			end

Otherwise, your main problem is that you're not explaining what's happening when you run your code! :P/>

That is actually working! Thanks! :D/>