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

Turning off -single- redstone outputs?

Started by Tassyr, 31 October 2012 - 11:57 PM
Tassyr #1
Posted 01 November 2012 - 12:57 AM
So I'm trying to make a computer menu where I can toggle on and off one of two (for now; Up to six later) redstone outputs.

Thing is, I can only figure out how to clear them -all,- so it'd shut things down.

Is there any special way to do this?

Additional information, in case it helps. I plan to put this all inside a 'while true do' loop that will run until the user inputs a quit command.
Kingdaro #2
Posted 01 November 2012 - 01:16 AM
You could just go through all of the computer's sides, and then rs.setOutput() them all false.

local sides = rs.getSides() -- this gets all of the sides available, easier than making a table with the sides in them
for _, side in pairs(sides) do
  rs.setOutput(side, false)
end
Tassyr #3
Posted 01 November 2012 - 01:55 AM
wait, wouldn't that turn them ALL off then? o.O I just want to deactivate/reactivate one at a time o.o
Kingdaro #4
Posted 01 November 2012 - 02:01 AM
Haha, I misread your post. So you want to toggle individual outputs, then? That's easier, just set the output you want to the opposite of what it currently is. The "not" keyword helps us out here.


local yourSide = 'back' -- or whatever side
rs.setOutput(yourSide, not rs.getOutput(yourSide))
Tassyr #5
Posted 01 November 2012 - 05:04 PM
Uh… what happened to the rest of this forum post…?
Kingdaro #6
Posted 01 November 2012 - 05:07 PM
…No seriously, what happened.

It was like…right there.
Lyqyd #7
Posted 01 November 2012 - 05:25 PM
We'll try this again. I believe the forums are in a state of transition at the moment.


function setBundledColor(side, color, state)
    if state then
	    if not colors.test(rs.getBundledOutput(side), color) then
		    rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side), color))
	    end
    else
	    if colors.test(rs.getBundledOutput(side), color) then
		    rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side), color))
	    end
    end
end
Tassyr #8
Posted 01 November 2012 - 05:30 PM
okay, lyqyd. Maybe I'm an idiot- entirely possible, considering how hard this has been- but I just don't understand how your function will work. o.O
Lyqyd #9
Posted 01 November 2012 - 05:31 PM
You just call it with the side, color, and desired state. To turn on the green wire on the back,

setBundledColor("back", colors.green, true)

Just like with the plain redstone functions, only you specify the color as well.
Tassyr #10
Posted 01 November 2012 - 05:37 PM
okay. Do I have to fill in 'side' and all that in the function itself, or just in the call?
Lyqyd #11
Posted 01 November 2012 - 05:39 PM
Just in the call! Whatever values you specify for the side, color, and state when you call it automatically get used. That's the nice part about having it as a function.
Tassyr #12
Posted 01 November 2012 - 05:44 PM
now last confused question XD the function call; it'll either say true -or- false. But I was hoping for a toggle. Do I just have to use two seperate inputs? I will if I -have- to but i was really hoping to make this as simplified as possible.
Tassyr #13
Posted 01 November 2012 - 06:10 PM
Correction, ignore what was here. Okay. so with my menu I can turn them on. How do I set up the conditions so that option 1 can both enable or disable something?
Kingdaro #14
Posted 01 November 2012 - 10:59 PM
I'll just rewrite what I had before - hopefully it won't get lost this time.

The way I'd do it is to just have two true/false variables to track the red and green colors, and set the rs bundled outputs accordingly. This script makes it so that the R key toggles red and the G key toggles green, just as an example.

local red = false
local green = false

while true do
  local _, key = os.pullEvent('key')

  -- toggle our variables on the keys pressed
  if key == keys.r then
    red = not red
  elseif key == keys.g then
    green = not green
  end

  -- add the colors to the sum of the rs output if they are on
  local sum = 0
  if green then sum = sum + colors.green end
  if red then sum = sum + colors.red end

  -- set the output
  rs.setBundledOutput('back', sum)
end
Lyqyd #15
Posted 02 November 2012 - 03:28 AM
Correction, ignore what was here. Okay. so with my menu I can turn them on. How do I set up the conditions so that option 1 can both enable or disable something?

I've got a snippet of code elsewhere on the forums that toggles the state of a specific color on a specific side, if that's what you're looking for. I believe it's around page six of my post history, or I can find it for you this evening. :D/>/>
Tassyr #16
Posted 02 November 2012 - 09:56 AM
Correction, ignore what was here. Okay. so with my menu I can turn them on. How do I set up the conditions so that option 1 can both enable or disable something?

I've got a snippet of code elsewhere on the forums that toggles the state of a specific color on a specific side, if that's what you're looking for. I believe it's around page six of my post history, or I can find it for you this evening. :D/>/>

Right, I don't really know how to access your history- I'm new to this forum -and- lua. XD could I beg you to repost it here? :)/>/> I'd like to compare to Kingdaro's to see which'll be easier for me to implement.
Lyqyd #17
Posted 02 November 2012 - 12:53 PM
Here's the toggle function. Call this with a side and a color and it will toggle that color on that side.


function toggleBundledColor(side, color)
    rs.setBundledOutput(side, (colors.test(rs.getBundledOutput(side), color) and colors.subtract(rs.getBundledOutput(side), color) or colors.combine(rs.getBundledOutput(side), color)))
end