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

How can i shorten this and Multiple color cables turned on?

Started by denni199, 30 January 2013 - 10:09 PM
denni199 #1
Posted 30 January 2013 - 11:09 PM
Title: How can i shorten this and Multiple color cables turned on?

I am making my little learning program where i am trying to have the user input what color of wire should be turned on.
As of yet it is long and i don't know of any way to shorten it:

local tArgs = {...}
input = tArgs[1]
color = nil

if input == "white" then
color = tonumber(1)
elseif input == "orange" then
color = tonumber(2)
etc...

rs.setBundledOutput("back", color)
sleep(2)
rs.setBundledOutput("back", 0)

Also how would i go about having the player input able to take multiple.
Right now you would write:

test white
to turn on the white cable.
But what if i wanted to turn on the white cable AND the orange cable?
Sure i could add more arguments but then i would need to make more if statements and it would all be a mess.
What could i do in my 2 problems here?
1lann #2
Posted 31 January 2013 - 03:42 AM
I'm on my iPod right now, try this.

local tArgs = {...}
local output = 0

for k, v in pairs(tArgs) do
if colors[v] then
 print("Added color " .. v)
 output = colors.combine(output, colors[v])
else
 print("Invalid Color: " .. v)
end
end

rs.setBundledOutput("back", output)
sleep(2)
rs.setBundledOutput("back", 0)
Quick explanation:
Since all the colors are stored in a table called colors, we can check if the argument has an index with value in the colors table. I loop through this through the arguments and keep on adding on to the output. Remember tArgs is actually just a table.

Good luck with your coding!
denni199 #3
Posted 31 January 2013 - 03:58 AM
I'm on my iPod right now, try this.

local tArgs = {...}
local output = 0

for k, v in pairs(tArgs) do
if colors[v] then
print("Added color " .. v)
output = colors.combine(output, colors[v])
else
print("Invalid Color: " .. v)
end
end

rs.setBundledOutput("back", output)
sleep(2)
rs.setBundledOutput("back", 0)
Quick explanation:
Since all the colors are stored in a table called colors, we can check if the argument has an index with value in the colors table. I loop through this through the arguments and keep on adding on to the output. Remember tArgs is actually just a table.

Good luck with your coding!
Sorry i don't fully understand.

It would be quite helpful if you could possibly explain what each individual piece of code does since i am still a beginner trying to learn :)/>
I don't get SOME of this:

for k, v in pairs(tArgs) do
if colors[v] then
print("Added color " .. v)
output = colors.combine(output, colors[v])
else
print("Invalid Color: " .. v)
end
end

It sort of works and sort of doesn't.
I can do a color at a time but not two colors at a time.
I am doing the following command which i am guessing is the correct one right?:

test white orange
I am assuming it is as it does say they both turn on but only white does.

I am able to put in jibberish and it accepts it and activates White.
1lann #4
Posted 31 January 2013 - 11:18 AM
Sorry I couldn't do a full explanation and bug test this. I'm still on my iPod. I'll get back to you once I'm on a computer. Though the results you are getting are pretty strange. Though then again I really don't try to use bundled cables.
Doyle3694 #5
Posted 31 January 2013 - 11:33 AM
EDIT: NVM, checked lua manual, I was wrong.

Can you give us the code you are currently using, implementing 1lann's idea?
KaoS #6
Posted 31 January 2013 - 12:04 PM
I use a basic code for adding colors to the current output

rs.setBundledOutput("back",colors.combine(rs.getBundledOutput("back"),colors.while))
for example would add white to the current output. basically you combine the current output with colors.white and set that as the output

if you were to use that in your code it would look like this

local tArgs = {...}
for k, v in pairs(tArgs) do
  if type(colors[v])=="number" then --to avoid adding functions and breaking stuff
    rs.setBundledOutput("back",colors.combine(rs.getBundledOutput("back"),colors[v]))
  end
end
sleep(2)
rs.setBundledOutput("back", 0)
denni199 #7
Posted 31 January 2013 - 12:52 PM
Okay thanks to everyone.
I figured it out :D/>