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

Auto Wrap

Started by plazter, 27 March 2016 - 04:11 AM
plazter #1
Posted 27 March 2016 - 06:11 AM
Hey Pros!
So a buddy and i, have been playing around for an auto wrapper for turbines and monitors, but when we plug the monitor into the system, it gives us error: 36:attempt to call nill

Code:
Spoiler

turbines = {}
monitors = {}

local function isMonitor(B)/>/>/>/>/>
  return b == "left"
	  or b == "right"
	  or b == "front"
	  or b == "back"
	  or b == "top"
	  or b == "bottom"
	  --or peripheral.getType(B)/>/>/>/>/> == "monitor"
end

print("Found devices: ")
-- Searches for devices
for a,b in ipairs(peripheral.getNames()) do

  -- prints the devices
  print(a ..": ".. B)/>/>/>/>/>

  -- if element is a turbine or a monitor
  if peripheral.getType(B)/>/>/>/>/> == "BigReactors-Turbine" then
	table.insert(turbines, peripheral.wrap(B)/>/>/>/>/>)
  elseif isMonitor(B)/>/>/>/>/> then
	table.insert(monitors, peripheral.wrap(B)/>/>/>/>/>)
	print("Monitor found ("..a..")")
  end

end

for i=1, #monitors do
  monitors[i].clear()
  monitors[i].write("*")
end

Any help is appreciated! :)/>
//Plazter

EDIT: the "(b )/>" is done by the editor.. dno why we use "(b )"
EDIT 2#:

turbines = {peripheral.find("BigReactors-Turbine")}
monitors = {peripheral.find("monitor")}
for i=1, #monitors do
  monitors[i].clear()
  monitors[i].write("*")
end
print ("Amount of turbines found: "..table.getn(turbines).."\n")
for i = 1, #turbines do
		monitors[1].write(turbines[1].getActive())
end
Line 10: "monitors[1].write(turbines[1].getActive())
is returning a "attempt to call nill" error, really cant see where its going wrong :(/> (if you read before i edited the edited the edited version, we have some snipped out part in the code ingame, typed without thinking :P/>)
Edited on 27 March 2016 - 05:07 AM
Bomb Bloke #2
Posted 27 March 2016 - 06:22 AM
Well, if any non-monitor is directly next to your system, then your isMonitor() function will erroneously return true for that device… this'd lead to your loop down the bottom erroring out.

It looks like you're really after peripheral.find():

local monitors = {peripheral.find("monitor")}
plazter #3
Posted 27 March 2016 - 06:26 AM
Well, if any non-monitor is directly next to your system, then your isMonitor() function will erroneously return true for that device… this'd lead to your loop down the bottom erroring out.

It looks like you're really after peripheral.find():

local monitors = {peripheral.find("monitor")}

That worked, thanks bloke!
plazter #4
Posted 27 March 2016 - 06:51 AM
Ok, we changed a code for a bit.. and it now looks like this:

turbines = {peripheral.find("BigReactors-Turbine")}
monitors = {peripheral.find("monitor")}

for i=1, #monitors do
  monitors[i].clear()
  monitors[i].write("*")
end
print ("Amount of turbines found: "..table.getn(turbines).."\n")
for i = 1, #turbines do
	monitors[1].write(turbines[1].getActive())
end

That now gives us an attempt to call nill :/
Edited on 27 March 2016 - 04:54 AM
Dog #5
Posted 27 March 2016 - 06:54 AM
On your first line, you're missing a closing quote and closing brackets/parenthesis

turbines = { peripheral.find("BigReactors-Turbine") }
plazter #6
Posted 27 March 2016 - 06:55 AM
On your first line, you're missing a closing quote and closing brackets/parenthesis

turbines = { peripheral.find("BigReactors-Turbine") }

just saw that, it was a typo :P/>, the error is still occuring.. line 10: Attempt to call nill
Edited on 27 March 2016 - 05:05 AM
Dragon53535 #7
Posted 27 March 2016 - 07:23 AM

monitors[1].write(turbines[1].getActive())
--#Should be
monitors[1].write(turbines[i].getActive())
At the same time, I would first make sure that such values exist.

if (turbines[i] and turbines[i].getActive) then
   monitors[1].write(turbines[i].getActive())
end
If nothing prints out then we know for a fact that you're for some reason not getting properly wrapped turbines, or that they don't have a .getActive method
plazter #8
Posted 27 March 2016 - 08:22 AM

monitors[1].write(turbines[1].getActive())
--#Should be
monitors[1].write(turbines[i].getActive())
At the same time, I would first make sure that such values exist.

if (turbines[i] and turbines[i].getActive) then
   monitors[1].write(turbines[i].getActive())
end
If nothing prints out then we know for a fact that you're for some reason not getting properly wrapped turbines, or that they don't have a .getActive method

we got it working, i had done the isActive ingame.. derp :>, and the if one u mention.. i like that idea :>
COOLGAMETUBE #9
Posted 30 March 2016 - 11:58 AM
I included a service into my Framework:

/os/services/deviceMountService:

local e, param1, param2, param3, param4, param5 = os.pullEvent()
if e == "peripheral" or e == "peripheral_detach" then
if not WicowsTools.compareTables(
  devices.all,
  peripheral.getNames()) then
  shell.run("/os/boot/registerDevices", "--quiet")
  shell.run("/os/boot/openRednet", "--quiet")
end
end

/os/boot/registerDevices:

devices = {}
devices.all = {}
local ol = peripheral.getNames()
local opt = {...}
for i = 1, #ol do
local oside = ol[i]
local otype = peripheral.getType(oside)
if #opt > 0 and opt[1] == "--quiet" then else
  print("Found \""..otype.."\" at \""..oside.."\"")
end
if not devices[otype] then
  devices[otype] = {}
end
table.insert(devices["all"], oside)
table.insert(devices[otype], oside)
if #opt > 0 and opt[1] == "--quiet" then else
  sleep(0.05)
end
end

/os/boot/openRednet:

if devices["modem"] then
for i = 1, #devices.modem do
  local side = devices.modem[i]
  if side == "top" or side == "left" or side == "right" or side == "bottom" or side == "back" or side == "top" then
   rednet.open(side)
   local opt = {...}
   if #opt > 0 and opt[1] == "--quiet" then else
	print("Rednet is now open on side "..side)
	sleep(0.05)
   end
  end
end
end


I called this with the parallel API:
/os/boot/settupServices:

local services = {}
svl = fs.list("/os/services")
for i = 1, #svl do
table.insert(services, function() while true do shell.run("/os/services/"..svl[i]) end end)
end
local resume = function()
shell.setPath(".:/os/programs"..string.sub(shell.path(),2))
shell.run("cd", "/")
shell.run("/os/wicows/lockscreen/main")
end
parallel.waitForAll(resume, table.unpack(services))


EDIT: Before i forgot, i used functions of my WicowsTools-API!
Edited on 30 March 2016 - 10:09 AM