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

Issue when calling setTextColor

Started by mah17, 24 December 2015 - 07:06 AM
mah17 #1
Posted 24 December 2015 - 08:06 AM
I've stared at this for a bit, and it's driving me crazy. I think I need some fresh eyes.

Using a stacktrace utility, I get this:


window:88: Expected number
  stack trace: window:88 window:267 window:272 window:88 window:267 window:272 analogwatch.lua:59 analogwatch.lua:91

The source is over on pastebin here: http://pastebin.com/hPtB0yu4. I reused a lot of code from my older projects, this one in particular that worked as-is sometime earlier this year: http://pastebin.com/H8iD2aw2

Any suggestions?
Bomb Bloke #2
Posted 24 December 2015 - 09:01 AM
That's odd, your trace seems to only be valid for your older bit of code?

Anyway, yeah, by default an advanced computer runs everything through multishell, which in order to support its tabbing system, renders everything through windows. These window objects don't check that they're being used correctly, so if you pass in something that'd make the term API crash, then the window API gets blamed.

Line 59 of your older "sorter" script is doing this:

windows.root.setTextColor(HEADER_FG_COLOR)

But "HEADER_FG_COLOR" is undefined, because around line 18 you're doing this:

if term.isColor() then
  local HEADER_BG_COLOR = colors.blue
  local HEADER_FG_COLOR = colors.white
else
  local HEADER_BG_COLOR = colors.white
  local HEADER_FG_COLOR = colors.black
end

By localising these variables to the inside of the "if" blocks, they get discarded as soon as those blocks end. One way around this is with a forward declaration:

local HEADER_BG_COLOR, HEADER_FG_COLOR
if term.isColor() then
  HEADER_BG_COLOR, HEADER_FG_COLOR = colors.blue, colors.white
else
  HEADER_BG_COLOR, HEADER_FG_COLOR = colors.white, colors.black
end
mah17 #3
Posted 26 December 2015 - 05:47 AM
Ah that makes sense. Thanks for the help!