Jump to content




Can I make these commands use variables?


  • You cannot reply to this topic
5 replies to this topic

#1 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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 by KingofGamesYami, 18 March 2014 - 07:22 PM.


#2 CometWolf

  • Members
  • 1,283 posts

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())

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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 by theoriginalbit, 17 March 2014 - 07:21 AM.


#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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 by KingofGamesYami, 18 March 2014 - 12:09 AM.


#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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


#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 18 March 2014 - 07:21 PM

View PostLyqyd, on 18 March 2014 - 12:26 AM, said:

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users