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

Error With colors Api

Started by dstars5, 07 April 2013 - 08:51 AM
dstars5 #1
Posted 07 April 2013 - 10:51 AM
Hey guys,

I am working on a disco room for my server, and this is the code I have so far. This was all working fine until I added the flashHalf() function, which tells me it might have to do with that. The error I get is:

startup:9: attempt to perform arithmetic __add on nil and nil

The code gets kind of messed up on here. Line 9 is where allColors is defined.

Here is the code:


side = "right"
local lime = colors.lime
local red = colors.red
local blue = colors.blue
local magenta = colors.magenta
local yellow = colors.yellow
allColors = lime + red + blue + magenta + yellow
function colorsOff()
rs.setBundledOutput(side, 0)
end
function colorsOn()
rs.setBundledOutput(side, allColors)
end
function limeOn()
rs.setBundledOutput(side, lime)
end
function redOn()
rs.setBundledOutput(side, red)
end
function yellowOn()
rs.setBundledOutput(side, yellow)
end
function blueOn()
rs.setBundledOutput(side, blue)
end
function magentaOn()
rs.setBundledOutput(side, magenta)
end
function slowCycle()
for i = 1, 4 do
  limeOn()
  sleep(1)
  colorsOff()

  redOn()
  sleep(1)
  colorsOff()

  blueOn()
  sleep(1)
  colorsOff()

  yellowOn()
  sleep(1)
  colorsOff()

  magentaOn()
  sleep(1)
  colorsOff()
end
end
function fastCycle()
for i = 1, 4 do
   limeOn()
   sleep(.5)
   colorsOff()
  
   redOn()
   sleep(.5)
   colorsOff()
  
   blueOn()
   sleep(.5)
   colorsOff()
  
   yellowOn()
   sleep(.5)
   colorsOff()
  
   magentaOn()
   sleep(.5)
   colorsOff()
end
end
function superFastCycle()
for i = 1, 4 do
   limeOn()
   sleep(.1)
   colorsOff()
  
   redOn()
   sleep(.1)
   colorsOff()
  
   blueOn()
   sleep(.1)
   colorsOff()
  
   yellowOn()
   sleep(.1)
   colorsOff()
  
   magentaOn()
   sleep(.1)
   colorsOff()
end
end
function flashHalf()
for i = 1, 4 do
  colors = {lime, red, yellow, blue, magenta}
  numLeft = 4

  rand1 = math.random(0, numLeft)
  newColor1 = colors[rand1]
  table.remove(colors, rand1)

  numLeft = numLeft - 1

  rand3 = math.random(0, numLeft)
  newColor3 = colors[rand3]
  table.remove(colors, rand3)

  numLeft = numLeft - 1

  rand2 = math.random(0, numLeft)
  newColor2 = colors[rand2]
  table.remove(colors, rand2)

  numLeft = numLeft - 1

  rand4 = math.random(0, numLeft)
  newColor4 = colors[rand4]
  table.remove(colors, rand4)

  numLeft = numLeft - 1

  rand5 = math.random(0, numLeft)
  newColor5 = colors[rand5]
  table.remove(colors, rand5)

  numLeft = numLeft - 1

  rs.setBundledOutput(side, newColor1 + newColor2 + newColor3)
  sleep(.5)
  colorsOff()
  rs.setBundledOutput(side, newColor4 + newColor5)
  sleep(.5)
  colorsOff()
end
end
function flashAll()
for i = 1, 4 do
  colorsOn()
  sleep(.5)
  colorsOff()
  sleep(.5)
end
end
while true do
flashHalf()
end

Anyone know why it stopped working?

Thanks!
dstars5 #2
Posted 07 April 2013 - 12:10 PM
Hmm, I seemed to have fixed it by assigning the colors their actual value, rather than using the color api. Anyone know why the color api was returning nil?
LBPHacker #3
Posted 07 April 2013 - 12:25 PM
What version of CC are you using? And next time (yeah, I know it should not matter) use colours.* (notice the "u") - I don't like that spelling but there's a slight chance that it would work. Other than that I have no idea and it has to be a very tough problem if this question is still unanswered.

EDIT: I'm blind. See the comment below…
Engineer #4
Posted 07 April 2013 - 12:31 PM
You are overriding the colors table in function halfFlash()
You should give that table another name.

color is safe to use :P/>