Jump to content




[Snippet] How to detect peripherals


14 replies to this topic

#1 3istee

  • New Members
  • 11 posts

Posted 11 March 2012 - 05:27 PM

Hey,
I am not sure if it is "enough" for a tutorial, but I would like to try ^^

I used the new peripheral API which came in 1.3 to detect if there is a modem and on which side it is.
Of course you could ask for any peripheral.

Spoiler

As you can see, it is really simple.
So I think this tutorial/snippet is mostly usefull for beginners.

#2 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 11 March 2012 - 05:44 PM

Hey 3istee,
in the name of efficiency and everyone's benefit I've tried to make the code a little bit more compact.
I hope you don't mind. Just trying to help. ;)/>
function hasPeripheral( sName )
  local tSides = redstone.getSides()

  for i = 1, #tSides do
	if peripheral.isPresent( tSides[i] ) and peripheral.getType( tSides[i] ) == sName then
	  return tSides[i]
	end
  end

end

Alternative (iterate through the table instead of accessing the index via a counter loop):
function hasPeripheral( sName )
 
  for _, sSide in pairs( redstone.getSides() ) do
    if peripheral.isPresent( sSide ) and peripheral.getType( sSide ) == sName then
	  return sSide
    end
  end
 
end


Disclaimer: Haven't tried the code in-game yet, but it should work. If you get any error, give me a holler. :mellow:/>

Edited by Espen, 11 March 2012 - 05:51 PM.


#3 3istee

  • New Members
  • 11 posts

Posted 11 March 2012 - 05:51 PM

Oh I have no problem with that :mellow:/>
I think it should work, too, I don't really want to copy the text, tough.

Do the letters before the variablenames (t,s,..) stand for the type of the variable?
And could you say me where locals are valid? ^^

#4 Sebra

  • Members
  • 726 posts

Posted 11 March 2012 - 06:02 PM

I think it can be peripheral.find( sName ) for peripheral API

#5 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 11 March 2012 - 06:03 PM

View Post3istee, on 11 March 2012 - 05:51 PM, said:

Oh I have no problem with that ;)/>
I think it should work, too, I don't really want to copy the text, tough.

Do the letters before the variablenames (t,s,..) stand for the type of the variable?
And could you say me where locals are valid? ^^
Yes exactly, I adopted the habit to add the type infront of the variable names. That is very helpful for finding bugs the bigger a program gets.^^
I mostly use these:
  • s - String
  • t - Table
  • n - Number
Local variables are accessible within the block they were defined in.
This includes all functions that you define within the same block, even nested functions.

Let me try an analogy:
Let's say we have a blue box and you declare a local variable within this blue box.
Then all boxes within that blue box can also access this variable.
But if the blue box itself is part of one ore more bigger boxes around itself, then these can't access the blue box's local variable.
Local variables can only be accessed downards, not upwards.

I hope the analogy didn't confuse more that it tried to clarify. :mellow:/>



View PostSebra, on 11 March 2012 - 06:02 PM, said:

I think it can be peripheral.find( sName ) for peripheral API
There is no find() function in the peripheral API.

Edited by Espen, 11 March 2012 - 06:04 PM.


#6 Sebra

  • Members
  • 726 posts

Posted 11 March 2012 - 06:15 PM

I mean it's worth to be added.

#7 Liraal

  • New Members
  • 477 posts
  • LocationPoland

Posted 11 March 2012 - 06:20 PM

i dont think it is, you can only have 6 peripherals (unless im wrong) so just look up all 6 sides for a match.

#8 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 11 March 2012 - 06:20 PM

View PostSebra, on 11 March 2012 - 06:15 PM, said:

I mean it's worth to be added.
Ah, sorry my bad. Misunderstood you then. :mellow:/>

#9 DrEckenbecker

  • New Members
  • 7 posts

Posted 04 May 2012 - 03:38 AM

I actually use this in my scripts to find rednet modems.
Sides = {"back","right","left","front","top","bottom"}
for i=1,#Sides do
if peripheral.isPresent(Sides[i]) then
  if peripheral.getType(Sides[i]) == "modem" then
   side = Sides[i]
  end
end
end
rednet.open(side)

its easier than having to remember where to put the stupid modem in refrence to the the program, and its only a few lines.

#10 yoskaz01

    Featured on Reddit!

  • New Members
  • 92 posts

Posted 04 May 2012 - 03:50 AM

I actually added a fun to the latest sensors API which returns a dictionary with all connected peripherals

that is for when u want to use more than one peripheral in your program

why loop again each time?

just loop once and get all I'm one go.

#11 Espiroth

  • New Members
  • 6 posts

Posted 29 May 2012 - 06:36 PM

function hasPeripheral(sName)
 local Sides = {"back","right","left","front","top","bottom"}
 int = 1
 repeat
  if peripheral.isPresent(Sides[i]) then
   if peripheral.getType(Sides[i]) == (sName) then 
    dside = Sides[i]
   end
  end
  int = int + 1
 until int == #Sides or sides ~= nil
 return side
end

When I try to asign the found peripheral to a varialbe for use in my programs it says that I tried to call NIL
Same goes for Espen's variation.

How do i assign the Sides[i] to a string for use later?

#12 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 29 May 2012 - 06:51 PM

Your problem is you're assigning the value of Sides[i] to dside and trying to return side. This code will be better though:

function getPeripheral(perType)
  for k,v in pairs(rs.getSides()) do
    if peripheral.getType(v) == perType then 
      return v
    end
  end
  return nil
end

It will return the side that the peripheral is on.

#13 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 29 May 2012 - 06:54 PM

You have to store it in a variable like this:
local side = hasPeripheral("name of the peripheral")
And then use it like:
peripheral.call(side, "Method", ...)
-- or
local p = peripheral.wrap(side)
p.<Method>(...)


#14 Espiroth

  • New Members
  • 6 posts

Posted 29 May 2012 - 08:04 PM

View PostCloudy, on 29 May 2012 - 06:51 PM, said:

Your problem is you're assigning the value of Sides[i] to dside and trying to return side. This code will be better though:

function getPeripheral(perType)
  for k,v in pairs(rs.getSides()) do
	if peripheral.getType(v) == perType then
	  return v
	end
  end
  return nil
end

It will return the side that the peripheral is on.


What do i do with a "returned side", is it a variable, how do i call it? The limit of my experience is pretty much:

sSide = "however you got a string input"
rednet.open(sSide)

Plus, albeit unrelated to the thread, what's all that fancy stuff with the k,v in pairs and return nil?


View PostMysticT, on 29 May 2012 - 06:54 PM, said:

You have to store it in a variable like this:
local side = hasPeripheral("name of the peripheral")
And then use it like:
peripheral.call(side, "Method", ...)
-- or
local p = peripheral.wrap(side)
p.<Method>(...)

peripheral.call(side, "Method", ...)
Is that phrase necessary for me to make use of the variable side, because i'm not sure how to use that phrase, let alone what "Method" means.

Would, for example, this work?
redstone.setBundledOutput(side, color)

Furthermore, you make it seem simple to store the side variable but I don't see where in the function the Sides[i] goes from a table to a string for me to store.

Thanks for the assist so far guys!

#15 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 29 May 2012 - 08:13 PM

In my function you'd do this:

side = getPeripheral("peripheraltype")

Then you'd use the peripheral however using that.
E.g. If you want to use a monitor you could wrap it using mon = peripheral.wrap(side) then call functions like mon.write("test").

The "k, v" stuff is to iterate through the sides table and check the sides one by one.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users