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

Error With Colors -Help appriciated

Started by afiolmahon, 28 May 2012 - 10:02 PM
afiolmahon #1
Posted 29 May 2012 - 12:02 AM
Im having difficulty with one error that keeps coming up in my code parallel :22: bit :40: bad argument: double expected , got nil

What the code does is it takes the color specified on (wire) and then it checks if it is on in the awires bundle.


function stat(wire)
local rslt = colors.test(tonumber(awires), wire
if rslt == true then return "True" end
if rslt == false then return "False" end
if rslt == nil then return tostring(awires) end
end

ive tested the bundle awires and it does have a value but for some reason it seems to have an error related to the second line of the provided code.

Thanks In advance for any help
MysticT #2
Posted 29 May 2012 - 12:12 AM
There's two possible problems:
1) the awires variable contains a value that can't be converted to a number.
2) the wire variable is nil, you are not giving parameters or giving a nil value to the function.
Change the function to this:

function stat(wire)
  if type(wire)  ~= "number" then
    error("Bad argument: number expected, got "..type(wire))
  end
  local nAwires = tonumber(awires)
  if not nAwires then
    error("awires is not a number.")
  end
  local rslt = colors.test(nAwires, wire)
  if rslt == true then
    return "True"
  elseif rslt == false then
    return "False"
  elseif rslt == nil then -- colors.test never returns nil, so this will never happen
    return tostring(awires)
  end
That should help you see the problem when it happens.
afiolmahon #3
Posted 29 May 2012 - 12:15 AM
Oh I think i see what my error is now, ill just put in a default value for wire because it starts off nil and thats why im getting an error.

Thanks for the help!

EDIT:
the code is for a GUI to display the status of the different wires so when it turns on it is nil and thats where the error is coming from

Here is the new working code:


function stat(wire)
if awires == nil then bwires = 0
else bwires = awires
end
local rslt = colors.test(bwires, wire)
if rslt == true then return "True" end
if rslt == false then return "False" end
end
my_hat_stinks #4
Posted 29 May 2012 - 08:41 AM
Hm… Was the problem not simply that you didn't close colors.test?
You had
local rslt = colors.test(tonumber(awires), wire

which should be
local rslt = colors.test(tonumber(awires), wire)

Or, if you still want the input validation, you could have
local rslt = colors.test( (awires or 0), wire)
or
local rslt = colors.test( (awires and (type(awires)=="number")) or 0, wire)

And still not have to modify any other lines :)/>/>