get colors.colro from io.read() and then change to the name to be used

Started by CreeperGoBoom, Dec 24, 2019, 06:29 PM

Previous topic - Next topic

CreeperGoBoom

Is this possible? Code should provide enough of an explanation.

print("What color should i use for bundled cable output. use colors.<color>, EG: colors.red"
color=io.read()

rs.setBundledOutput("bottom",color)

Using tonumber on color doesn't do it. How do i convert the string "colors.white" into the color name as not a string?

Wojbie

I am guessing from your input that you want to write "colors.red" and have program use value of red from colors api right?

Simplest way would be to create a lookup table that would have values you are using stored like per example
lookup = {
"colors.red" = colors.red
"colors.blue" = colors.blue
.. ect
}

print("What color should i use for bundled cable output. use colors.<color>, EG: colors.red")
color=lookup[io.read()]

rs.setBundledOutput("bottom",color)

But that is basically just rewriting the information that already exists. IF you asked the end user to just specify color like "blue"

print("What color should i use for bundled cable output. use <color>, EG: red")
color=colors[io.read()]

rs.setBundledOutput("bottom",color)

That does have issue of what if user enters a value that is a function not a color number tho.

Does that answer your rather open ended question?

CreeperGoBoom


CreeperGoBoom

Quote from: Wojbie on Dec 24, 2019, 09:25 PMprint("What color should i use for bundled cable output. use <color>, EG: red"
color=colors[io.read()]

rs.setBundledOutput("bottom",color)

That does have issue of what if user enters a value that is a function not a color number tho.

Is that for a specific version of CC?

I get expected string, number error with that...

Wojbie



Fast test shows that it works well. error has to be somewhere else in code.

I noticed that your print is lacking the closing parenthesis ")" so i am guessing that's your issue?

For future reference its best to post code you are using and error so people can know exactly where its erroring and not guess.

CreeperGoBoom

That now works funnily enough. is there just as easy a way as that to print/return color name?

Wojbie

By default not. Here you could use my first suggestion and make a lookup table but going other direction with [colors.red] = "red" ect. You can easily lookup colors at wiki https://wiki.computercraft.cc/Colours_API.

I am seeing that you are using that to interact with bundled Inputs/Outputs right? You do realize that if per example 2 colors are lit at once then the number will not be any of separate ones but combination of them. So my proposed approach would only work if you are only using one color at time and for combined colors system would require more sophisticated code.