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

Redpower Bundled cable

Started by kingcoon, 27 December 2012 - 12:54 PM
kingcoon #1
Posted 27 December 2012 - 01:54 PM
hey guys i need some help, i cant seem to get this to work.

i need to subtract only one color from a set of Red-power bundles outputs, how do i do that?


program:

defines variable : lightsState = "off"

with a command it changes between on and off within an infinite loop,

how change it from on to off, i can set it to on, but i don't know how to turn it off without turning everything else off


thanks,

kingcoon
ChunLing #2
Posted 27 December 2012 - 03:33 PM
You…er, you subtract it.

Okay, I guess your real question might be, how do you know what to subtract it from? That would be something like:
rs.getBundledOutput("back",colors.subtract(rs.getBundledOutput("back"), colors.blue))
If you wanted to turn off the blue on the back
Edited on 27 December 2012 - 02:38 PM
kingcoon #3
Posted 27 December 2012 - 06:32 PM
thanks
Luanub #4
Posted 27 December 2012 - 06:42 PM
Another way is to use the colors API in conjunction with the redstone api. If you type help redpower from the terminal it will give you more information. Here is a short example


local c = 0 -- just declares c, 0 means all colors off.

--to add a color and turn it on do
c = colors.combine(c, colors.white)
rs.setBundledOutput("back", c) --white is now on

--lets add red to the mix
c = colors.combine(c, colors.red)
rs.setBundledOutput("back", c) --red and white are now on

--now lets remove white
c = colors.subtract(c, colors.white)
rs.setBundledOutput("back", c) --now only red is on

--everything off
rs.setBundledOutput("back", 0 )

This is probably one of the least error prone methods of handling bundled cables.
AngelMalus #5
Posted 27 December 2012 - 08:15 PM
Remember colors are just number values. 1 for white, 2 for orange 4 for magenta and so on.
RedPower computers use them like that and they work the same in ComputerCraft.
If you set the output to 3 it will turn power in 2 lines (white and orange) if you just want to turn off the orange, you substract its value from the output.
So 3 - 2 = 1, and set 1 as the new output. Something like this:

rs.setBundledOutput("back",3) – will turn 2 lights on on white and orange
rs.setBundledOutput("back",1) – will turn off orange light, leaving white on

These are the basic 2 colors, the rest folows as 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048….. until black. The color order is the same you see in NEI (not enough items menu on the right side screen of your inventory)

Of course the colors API handles it a bit easier with the colors.substract
Just wanted to extend the info on the color subject.
Luanub #6
Posted 27 December 2012 - 08:40 PM
Remember colors are just number values. 1 for white, 2 for orange 4 for magenta and so on.
RedPower computers use them like that and they work the same in ComputerCraft.
If you set the output to 3 it will turn power in 2 lines (white and orange) if you just want to turn off the orange, you substract its value from the output.
So 3 - 2 = 1, and set 1 as the new output. Something like this:

rs.setBundledOutput("back",3) – will turn 2 lights on on white and orange
rs.setBundledOutput("back",1) – will turn off orange light, leaving white on

These are the basic 2 colors, the rest folows as 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048….. until black. The color order is the same you see in NEI (not enough items menu on the right side screen of your inventory)

Of course the colors API handles it a bit easier with the colors.substract
Just wanted to extend the info on the color subject.

The only problem with handling it by numbers is that if you had a variable that holds the number for the colors you want on, and say you want white on so you set the value to 1. Later on in the script you go to turn white on again so you add 1 to the value, instead of getting white you now get orange because the value is now 2. It doesn't know that white was already in the value and blindly adds it, if you're not really on it this can cause the scripts to not run properly.

Example:

local c = 0
-- lets turn on white
c = c + 1
rs.setBundledOutput("back", c) -- white is on
-- then lets do it again
c = c + 1
rs.setBundledOutput("back", c) -- now orange is on and white is off, not the desired output

--a better way
c = colors.combine(c, colors.white)
rs.setBundledOutput("back", c) -- white is on
--again
c = colors.combine(c, colors.white) --since white was already added to c the colors API does not add it again.
rs.setBundledOutput("back", c) -- white stays on
Kingdaro #7
Posted 27 December 2012 - 08:43 PM
May I add? Just a nice little tip, something like this is usually made really easy with a nice subtraction/addition function, so you can just say "add(something)" or "subtract(something)" to make things easier for yourself in the future.


local side = "right" -- or whatever your bundled input is

function add(color)
  local input = rs.getBundledInput(side)
  rs.setBundledInput(side, colors.combine(input, color))
end

function subtract(color)
  local input = rs.getBundledInput(side)
  rs.setBundledInput(side, colors.subtract(input, color))
end

add(colors.red)
add(colors.white)
subtract(colors.white)

-- we are left with red
-- :D/>