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

Small issue with wrapping peripheral

Started by Caleb Ragnarok, 12 December 2015 - 07:42 PM
Caleb Ragnarok #1
Posted 12 December 2015 - 08:42 PM
Im getting an error on line 25 thats states, Incorrect arguments! Enter a boolean or an Integer 0-15.


local Emitters = {}
local EmitterCount = 0
local DefenseScreen = {"io_1", "io_2"}
local DefenseCount = 1
local State = { ... }
local Active = nil

if State[1] == "Defense_on" then
  Active = true
  print(Active)
  defense(Active)
elseif State[1] == "Defense_off" then
  Active = false
  defense()
else
  
end 

print(Active) 

function defense(Active)
  i = 1
  print("Defense Screens Activated.")
  while i <= DefenseCount do
   Emitter = peripheral.wrap(DefenseScreen[i])
   Emitter.setOutput(Active)
    i = i + 1
  end 
end
Gumball #2
Posted 12 December 2015 - 09:06 PM
Im getting an error on line 25 thats states, Incorrect arguments! Enter a boolean or an Integer 0-15.


local Emitters = {}
local EmitterCount = 0
local DefenseScreen = {"io_1", "io_2"}
local DefenseCount = 1
local State = { ... }
local Active = nil

if State[1] == "Defense_on" then
  Active = true
  print(Active)
  defense(Active)
elseif State[1] == "Defense_off" then
  Active = false
  defense()
else
  
end

print(Active)

function defense(Active)
  i = 1
  print("Defense Screens Activated.")
  while i <= DefenseCount do
   Emitter = peripheral.wrap(DefenseScreen[i])
   Emitter.setOutput(Active)
	i = i + 1
  end
end

Not too sure, but try moving Defense above the part where it gets called. I don't think Lua loads variables then compiles/runs
Dragon53535 #3
Posted 12 December 2015 - 09:12 PM
Not too sure, but try moving Defense above the part where it gets called. I don't think Lua loads variables then compiles/runs
That will fix a SINGLE error, however that will not fix the error he's wanting.


function defense(Active)
  i = 1
  print("Defense Screens Activated.")
  while i <= DefenseCount do
   Emitter = peripheral.wrap(DefenseScreen[i])
   Emitter.setOutput(Active)
		i = i + 1
  end
end
That Emitter.setOutput(Active) line is causing your error. because you're just trying to set it to a true or false. however at


elseif State[1] == "Defense_off" then
  Active = false
  defense()
else

end
you're calling defense with no parameters. thus Active (for the function) becomes nil, and WHOOPS, you get your error.

My suggestion, get rid of the active variable at the top entirely, and do this:


local Emitters = {}
local EmitterCount = 0
local DefenseScreen = {"io_1", "io_2"}
local DefenseCount = 1
local State = { ... }

function defense(Active) --#I moved defense up top, because as Bluebird said, it will cause errors. Which you probably know
  if Active == nil then --#This will make damn sure Active isn't nil
	Active = false
  end
  i = 1
  print("Defense Screens Activated.")
  while i <= DefenseCount do
   Emitter = peripheral.wrap(DefenseScreen[i])
   Emitter.setOutput(Active)
		i = i + 1
  end
end
if State[1] == "Defense_on" then
  defense(true) --#Notice the true false calls on these. It's setting the defense functions local variable Active to true or false.
elseif State[1] == "Defense_off" then
  defense(false)
else

end

As an extra tip, I would wrap the peripherals into another table, and then reference that whenever you want to use the peripherals rather than wrapping them every time you want to use them.

local defenses = {}
local defensescreen = {"io_1","io_2"}
for i = 1,#defensescreen do
  defenses[i] = peripheral.wrap(defensescreen[i])
end
defenses[1].setOutput(true)

ps: Don't swap languages without remembering the correct commenting calls… Darn Java and C++ with your // comments…
Edited on 12 December 2015 - 08:17 PM