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

Updating the Peripheral API

Started by MindenCucc, 23 March 2013 - 02:24 AM
MindenCucc #1
Posted 23 March 2013 - 03:24 AM
Hello! I wrote some "USEFUL" function, that must be added into the peripheral API.


function getTypes(sType)
local fancyCrap = {}
local net = {}
local ret = {}
if sType == nil or type(sType) ~= "string" then
  error("Cannot parse input.", 1)
else
  net = peripheral.getNames()
  for asdX, valX in ipairs(net) do
   for netX = 1, #net do
    if peripheral.getType(valX) == sType then
	 retval=true
    else
	 retval=false
    end
   end
   if retval then
    table.insert(ret, valX)
   end
  end
  return ret
end
end


Example (usage):

getTypes(string PeripheralType)

function getMonitors()
local monitors=getTypes("monitor")
return monitors
end

Output: {"monitor_1", "monitor_5", "left", "right", "bottom"}
Cranium #2
Posted 23 March 2013 - 03:28 AM
I don't think the devs are going to add this to the APIs built into CC just because it's something that any user could do. If you want APIs like this, you can put it into your own copy.
Kingdaro #3
Posted 23 March 2013 - 03:28 AM
Eh. I think something like this can be easily done by the user, and doesn't need to be implemented by default.


local function getTypes(filter)
  local names = peripheral.getNames()
  for i=#names, 1, -1 do
    if peripheral.getType(names[i]) ~= filter then
      table.remove(names, i)
    end
  end
  return names
end
theoriginalbit #4
Posted 23 March 2013 - 03:32 AM
Yeh I don't see this being added… especially when you seem to be demanding it by saying 'that must be added'

Also I really don't understand your double looping with your code. as an alternative to the way Kingdaro supplied, and a method that is more similar to the one you used have a read of this code.



function getTypes( t )
  if type(t) ~= 'string' then error('Invalid parameter: expected string got, '..type(t), 2) end  
  local r = {}
  for _,v in pairs(peripheral.getNames())
    if peripheral.getType(v) == t then
      table.insert(r, v)
    end
  end
  return r
end
Cloudy #5
Posted 23 March 2013 - 03:55 AM
Nah. Do it yourself if you need to.