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

Can I make these commands use variables?

Started by KingofGamesYami, 17 March 2014 - 03:37 AM
KingofGamesYami #1
Posted 17 March 2014 - 04:37 AM
I'm currently trying to make a program that simplifies advanced monitors for people who can't program. Here's what I have so far (too lazy to type all the stuff where "…" is shown).

m = peripheral.wrap(side)
...
size = read()
m.setTextScale(size)
...
color = read()
m.setTextColor(color)
...
color2 = read()
m.setBackgroundColor(color2)
...
This code is the part that is protesting my use of variables. For some reason, they expect numbers, and even when I give my program a number to use, it still messes up. I think the reason is this:

color2 = read()
m.setBackgroundColor(color2) | m.setBackgroundColor("1")
where it is getting quotes. I don't know how I would fix this, unless I specifically input a bunch of options in an if, elseif sequence like this.

if color2 == "red" then
color2 = colors.red
elseif color2 == "blue" then
color2 = colors.blue
Here is my final code:
http://pastebin.com/ii02ZH2g
Edited on 18 March 2014 - 06:22 PM
CometWolf #2
Posted 17 March 2014 - 05:34 AM
The string reprensentation of a number and a regular number is not the same. Read always returns a string, to fix this, use tonumber(read())
theoriginalbit #3
Posted 17 March 2014 - 07:20 AM
however the problem with what CometWolf suggested lies in if the user types something like "blue" then that cannot be converted into a number, as such you must write some way to parse this; I was made a parser for my ccConfig graciously by LBPHacker, you can have a read up more of what it does here in the commit description, and read up on all the values it will parse here in the developer documentation. If you do have any questions feel free to ask, I do have an understanding of how it works and can explain it to you; I'm even currently trying to improve upon it and make it more efficient.
Edited on 17 March 2014 - 06:21 AM
KingofGamesYami #4
Posted 18 March 2014 - 12:08 AM
Theoretically, couldn't I just do this?

"white" = 1
"orange" = 2
--and so on
color = read()
peripheral.wrap("right").setTextColor(tonumber(color))
Edited on 17 March 2014 - 11:09 PM
Lyqyd #5
Posted 18 March 2014 - 12:26 AM
That's not gonna do what you think it's going to do. You could try allowing names by checking if the string is present as a key in either the colors or colours tables.


local input = read()
local color
if (colors[input] and type(colors[input]) == "number") or (colours[input] and type(colours[input]) == "number") then
  color = colors[input] or colours[input]
elseif tonumber(input) then
  color = tonumber(input)
else
  print("not a number or color!")
end
KingofGamesYami #6
Posted 18 March 2014 - 07:21 PM
That's not gonna do what you think it's going to do. You could try allowing names by checking if the string is present as a key in either the colors or colours tables.


local input = read()
local color
if (colors[input] and type(colors[input]) == "number") or (colours[input] and type(colours[input]) == "number") then
  color = colors[input] or colours[input]
elseif tonumber(input) then
  color = tonumber(input)
else
  print("not a number or color!")
end
That works, thanks.