Posted 22 July 2013 - 08:13 AM
I have been viewing a lot of posts and code here and for the most part, most applications hardwire which side a specific peripheral has to be on. For the most part, this is most likely due to the author just setting it up quick for his or her purpose.
This little code snippet can automatically detect which side the peripheral is attached to and return the side thus transforming a hardwired side to a customizable feature. Added bonus is that you don't have to mind which side it has to sit on and that you don't have to edit the code if you change the side.
Ok. Now that we have something that can detect which side a specific peripheral is connected on, it would be nice to know how to acutally use this thing. Coming right up …
In this example I will use a modem just for the fun of it.
That's pretty much it. Using this method, you can customize which side the peripheral is attached to and you don't have to worry about it anywhere else in your code.
Your code no longer has to rely on any device being attached to any specific side of the computer.
Just for the fun of it, here's a nifty way you can use the .getDeviceSide() function to detect a crafty turtle.
For those wanting the code without comments…
Happy coding :)/>
EDIT: Altered the text slightly, because the forum color coded wrong and dumped a bunch of HTML end-tag codes.
This little code snippet can automatically detect which side the peripheral is attached to and return the side thus transforming a hardwired side to a customizable feature. Added bonus is that you don't have to mind which side it has to sit on and that you don't have to edit the code if you change the side.
local function getDeviceSide(deviceType)
-- List of all sides
local lstSides = {"left","right","up","down","front","back"};
-- Now loop through all the sides
-- "i" holds a table counter
-- "side" will hold the actual side text
for i, side in pairs(lstSides) do
if (peripheral.isPresent(side)) then
-- Yup, there is something on this side
-- Check the type against the device
-- string.lower() just ensures that the passed
-- device type is in lower case.
-- peripheral.getType() always returns lower case texts
if (peripheral.getType(side) == string.lower(deviceType)) then
-- Yes, this is the device type we need, so return the side
return side;
-- Note, that this call to "return" also terminates
-- further running of this function
end -- if .getType()
end -- if .isPresent()
end -- for-do
-- If we reach this point, it means that we didnt find
-- the specified device anywhere, so return nil indicating
-- that it doesnt exist.
return nil;
end -- function()
Ok. Now that we have something that can detect which side a specific peripheral is connected on, it would be nice to know how to acutally use this thing. Coming right up …
In this example I will use a modem just for the fun of it.
-- Where is my modem connected, if any?
local modemSide = getDeviceSide("modem");
if (modemSide) then
-- We did get a side, so there is a modem connected
rednet.open(modemside);
else
print("Please attach a modem!")
end
-- ...
-- ...
if (modemSide) then
-- Close up the connection again
rednet.close(modemSide);
end
That's pretty much it. Using this method, you can customize which side the peripheral is attached to and you don't have to worry about it anywhere else in your code.
Your code no longer has to rely on any device being attached to any specific side of the computer.
Just for the fun of it, here's a nifty way you can use the .getDeviceSide() function to detect a crafty turtle.
local wbSide = getDeviceSide("workbench")
if (wbSide and turtle) then
print("Yay! I'm a Crafty Turtle.");
end
For those wanting the code without comments…
Spoiler
local function getDeviceSide(deviceType)
local lstSides = {"left","right","up","down","front","back"};
for i, side in pairs(lstSides) do
if (peripheral.isPresent(side)) then
if (peripheral.getType(side) == string.lower(deviceType)) then
return side;
end
end
end -- for-do
return nil;
end
Happy coding :)/>
EDIT: Altered the text slightly, because the forum color coded wrong and dumped a bunch of HTML end-tag codes.
Edited on 22 July 2013 - 06:24 AM