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

Color changing via variables?

Started by mrpoopy345, 03 December 2013 - 03:29 PM
mrpoopy345 #1
Posted 03 December 2013 - 04:29 PM
What I want to do is make the computer ask you for a color:

local color = read()
Then set the background color to that variable.
What I am using:

term.setBackgroundColor(color)
But it says expected number.
Any help?
Kingdaro #2
Posted 03 December 2013 - 04:49 PM
It doesn't quite work like that. read() returns a string, and if you typed "colors.red", you would get "term.setBackgroundColor("colors.red")", and not "term.setBackgroundColor(colors.red)"

What you should do is take the input string, then access the colors table using the string.

local color = read()
term.setBackgroundColor(colors[color])

Note that you would be typing "red" instead of "colors.red" for this method. If you typed in "red":

local color = "red"
term.setBackgroundColor(colors["red"]) --# colors["red"] is the same as colors.red, as colors["blue"] is the same as colors.blue