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

Setting text scale, background colour and text colour to a variable

Started by kobkob1, 27 June 2015 - 11:11 AM
kobkob1 #1
Posted 27 June 2015 - 01:11 PM
I might seem like a really big noob asking for this but I am working on a monitor setup stage in a piece of program that I am working on.
Here is my code for the setup part

attachedMonitor = peripheral.wrap("right")
print("What text size do you want?")
local textscale = read()
monitor.setTextScale(textscale)
print("What background colour do you want?")
local backgroundcolour = read()
attachedMonitor.setBackgroundColor(colors.backgroundcolour)
print("What text colour do you want?")
local textcolour = read()
attachedMonitor.setTextColor(colors.textcolour)
print("Setup Complete")
 
I run this code and after I have put in 1 for my textscale it says attempt to index ?( a nil value)
any help? Thanks
Exerro #2
Posted 27 June 2015 - 01:40 PM
When giving an error, please give the line number too. From what I can tell, you're trying to index the variable 'monitor' on line 4 instead of 'attachedMonitor'. As 'monitor' is nil, it's throwing an error instead of indexing it (because you can't index nil).

To fix that, change line 4 to:
attachedMonitor.setTextScale(textscale)

However, I'm not sure if that will work because it's expecting a number, and read returns a string. While Lua treats numbers and strings the same in most cases, it's worth converting it just in case:
attachedMonitor.setTextScale(tonumber(textscale))

Below, you're also trying to do 'colors.textcolour'. This simply won't work. Instead, use
attachedMonitor.setTextColor(colors[textcolour])
and the same again for background colour.

Writing 'a.b' is the same as writing 'a["b"]', and by writing 'colours.textcolour', you're actually writing 'colours["textcolour"]' (aka looking for the "textcolour" index inside the 'colours' table.
Edited on 27 June 2015 - 11:42 AM
kobkob1 #3
Posted 27 June 2015 - 02:32 PM
When giving an error, please give the line number too. From what I can tell, you're trying to index the variable 'monitor' on line 4 instead of 'attachedMonitor'. As 'monitor' is nil, it's throwing an error instead of indexing it (because you can't index nil).

To fix that, change line 4 to:
attachedMonitor.setTextScale(textscale)

However, I'm not sure if that will work because it's expecting a number, and read returns a string. While Lua treats numbers and strings the same in most cases, it's worth converting it just in case:
attachedMonitor.setTextScale(tonumber(textscale))

Below, you're also trying to do 'colors.textcolour'. This simply won't work. Instead, use
attachedMonitor.setTextColor(colors[textcolour])
and the same again for background colour.

Writing 'a.b' is the same as writing 'a["b"]', and by writing 'colours.textcolour', you're actually writing 'colours["textcolour"]' (aka looking for the "textcolour" index inside the 'colours' table.
Thanks. Yeah, the error was on line 4. Forgot to put that part in.