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

[Lua][Error] bit:40: bad argument: double expected, got nil

Started by ziah5, 30 November 2012 - 01:47 PM
ziah5 #1
Posted 30 November 2012 - 02:47 PM
I'm trying to make a computer that controls some RedPower2 lamps with bundled cable when I start the program everything works fine and the lamps turn on but when I close the computer screen it stops and gives this error: bit:40: bad argument: double expected, got nil.

Here is my code:

local names = {"Floor 1", "Floor 2", "Floor 3", "Floor 4"}
local keycolors = {colors.orange, colors.white, colors.yellow, colors.black}
local curcolors = 0
function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
while 1
do
	term.clear()
	term.setCursorPos(1, 1)
	for cnt = 1, tablelength(keycolors) do
		local status = "Off"
		if colors.test(curcolors, keycolors[cnt]) then
			status = "On"
		end
		print("[" .. cnt .. "] " .. names[cnt] .. ": " .. status)
	end
	evt, one, two, three = os.pullEvent()
	if evt == "key" then
		if (one - 1) <= tablelength(keycolors) then
			if colors.test(curcolors, keycolors[one - 1]) then
				colors.subtract(curcolors, keycolors[(one - 1)])
			else
				curcolors = colors.combine(keycolors[(one - 1)], curcolors)
			end
		end
	end
	rs.setBundledOutput("bottom", curcolors)
	sleep(0.3)
end

If anyone can see what I did wrong please let me know.
remiX #2
Posted 30 November 2012 - 03:04 PM
Is that your full code?

The error is bit:40: bad argument: double expected, got nil.
- Which shows us that your program name is called 'bit'
- The error is on line '40'

But your program only has 32 lines.
Orwell #3
Posted 30 November 2012 - 03:06 PM
The bit api is being called from colors.test(). Instead of using your own tablelength(), do this:

for cnt = 1,#keycolors do
Clearly, you call colors.test() with a nil value.
Luanub #4
Posted 30 November 2012 - 03:08 PM
bit:40 means that the bit API failed on line 40. Take a look at the bit api and see what it is doing on line 40. I'm assuming that this is probably caused by your colors functions, but I can't say for sure as I'm not to where I can test it.
Orwell #5
Posted 30 November 2012 - 03:11 PM
This is where the error might be:

evt, one, two, three = os.pullEvent()
  if evt == "key" then
    if (one - 1) <= tablelength(keycolors) then
	  if colors.test(curcolors, keycolors[one - 1]) then
...
The variable will just be a number representing the key pressed. You have no certainty that the variable 'one' isn't 1 or 0.
ziah5 #6
Posted 01 December 2012 - 04:14 AM
I think that the curcolors variable is the one that was nil but I'm not sure.
ziah5 #7
Posted 01 December 2012 - 04:19 AM
Ok thanks for helping me I got it working I had to make sure (one - 1) is greater than zero.

evt, one, two, three = os.pullEvent()
  if evt == "key" then
	if (one - 1) <= tablelength(keycolors) and (one - 1) > 0 then
		  if colors.test(curcolors, keycolors[one - 1]) then
...
Orwell #8
Posted 01 December 2012 - 07:34 AM
Ok thanks for helping me I got it working I had to make sure (one - 1) is greater than zero.

evt, one, two, three = os.pullEvent()
  if evt == "key" then
	if (one - 1) <= tablelength(keycolors) and (one - 1) > 0 then
		  if colors.test(curcolors, keycolors[one - 1]) then
...
Indeed. :)/> Well done!