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

101 school boy error with a function (I think) [solved]

Started by lebalusch, 01 June 2014 - 09:32 PM
lebalusch #1
Posted 01 June 2014 - 11:32 PM
–Not sure why this is throwing an error:



local Pcable= "back"
local cable = "white"
local ColourCable = ("colors.",cable)


function cableCombine()
redstone.setBundledOutput(Pcable,colors.combine(redstone.getBundledOutput(Pcable),ColourCable))
end

function cableSubtract()
redstone.setBundledOutput(Pcable,colors.subtract(redstone.getBundledOutput(Pcable),ColourCable))
end



so thats the functions then I try to call it using

cable= "blue"
cableCombine()
Edited on 29 June 2014 - 04:34 PM
GamerNebulae #2
Posted 01 June 2014 - 11:41 PM
What error is it throwing? Wild guess:
colors.<color> is not a string. It's an integer actually.
Whitecatblack #3
Posted 01 June 2014 - 11:44 PM
So I'm not entirely sure what you are trying to accomplish, but I will fix what I see.

What are you trying to do with this variable: ColourCable = ("colors.",cable) ?
Whatever it is, the way you are trying to currently do it is incorrect. Are you trying to have ColourCable = the function colors.<cable>() ?

Whitecatblack
Anavrins #4
Posted 02 June 2014 - 12:52 AM
Change, local ColourCable = ("colors.",cable)
To, local ColourCable = colors[cable]
Edited on 01 June 2014 - 10:52 PM
GamerNebulae #5
Posted 02 June 2014 - 03:08 PM
Change, local ColourCable = ("colors.",cable)
To, local ColourCable = colors[cable]

So the colors in CC are stored in tables? That would save me like a line or 16 in my program… lol.
Bomb Bloke #6
Posted 02 June 2014 - 04:11 PM
Every API is a collection of functions and/or other types of variables stored in a table.
flaghacker #7
Posted 02 June 2014 - 04:33 PM
Change, local ColourCable = ("colors.",cable)
To, local ColourCable = colors[cable]

So the colors in CC are stored in tables? That would save me like a line or 16 in my program… lol.

Yea, there's a defauld color API:

term.setBackgroundColor(colors.blue)
The API contains all 16 minecraft wool/dye colors.
Edited on 02 June 2014 - 02:34 PM
TheOddByte #8
Posted 02 June 2014 - 05:46 PM
I'm not sure, but you should try changing this part

local cable = "white"
local ColourCable = ("colors.",cable) -- I'm not sure what you were trying todo here .-.
into this


local cable = "white"
local ColourCable = colors[cable] -- get the index 'white' in the colors table
Cranium #9
Posted 02 June 2014 - 06:37 PM
You can actually use something like this to get any color in the api, without having to use tons of different functions/tables:


local function returnCol(key)
if key > 1 then return 2^(key - 2) else return 1 end
end
Just give it a number 1-15 and it will return the proper color.
Whitecatblack #10
Posted 02 June 2014 - 06:53 PM
Cranium said:
-snip-
This seems overly complex, but maybe I am missing the purpose of what you are trying to accomplish with your code. If needed, colors.key or colors[key] can be called very easily for almost any purpose.

Whitecatblack
TheOddByte #11
Posted 02 June 2014 - 09:14 PM
You can actually use something like this to get any color in the api, without having to use tons of different functions/tables:


local function returnCol(key)
if key > 1 then return 2^(key - 2) else return 1 end
end
Just give it a number 1-15 and it will return the proper color.
It seems like you messed up a little with that code :P/>
Just give it a number 1-15

if key > 1 then ... end
With that piece of code it would have to be between 2-15
Lyqyd #12
Posted 02 June 2014 - 10:06 PM
2 - 17, oddly enough.

Probably easier to just:


function selectColor(num)
  return 2 ^ (num - 1)
end

The above would expect values 1-16, whole numbers only.
lebalusch #13
Posted 02 June 2014 - 10:53 PM
The functions were a way of myself shortening down a rather long way of turning cables on and off. I just want a way of making bundled cables being turned on and off easily.
making it as simple as.

cable = "orange"
CableCombine()

open to suggestions for other ways.
Whitecatblack #14
Posted 02 June 2014 - 11:15 PM
lebalusch said:
open to suggestions for other ways.

Well, if I try to keep it relatively close to the original code you posted, here is how I would do it:

local Pcable = "back"
local cableColor = nil

function cableCombine(cableColor)
  redstone.setBundledOutput(Pcable,colors.combine(redstone.getBundledOutput(Pcable),colors[cableColor]))
end

function cableSubtract(cableColor)
  redstone.setBundledOutput(Pcable,colors.subtract(redstone.getBundledOutput(Pcable),colors[cableColor]))
end

Then all you would need to enter would be something like:

cableCombine("orange")
or

cableSubtract("orange")

Whitecatblack
Edited on 03 June 2014 - 12:53 PM
Cranium #15
Posted 03 June 2014 - 02:44 PM
Well, I derped then. I'll admit it. My maths aren't that great. Thanks for correcting me.