4 posts
Posted 05 September 2012 - 02:27 AM
The first (and only as of now) Api that I have is to automatically detect any monitors on any side and assign the 'mon' variable to them eg. mon.write() .
If you have a custom api you would like post here or send me a pm and i'll see what I can do.
The paste bin is
http://pastebin.com/SEW4jmmR
318 posts
Location
Somewhere on the planet called earth
Posted 06 September 2012 - 02:02 PM
This could be made much better:
mSides = {}
mSides[1] = "top"
mSides[2] = "bottom"
mSides[3] = "front"
mSides[4] = "back"
mSides[5] = "right"
mSides[6] = "left"
for ms = 1, 6 do
if peripheral.getType(mSides[ms]) == "monitor" then
mon = peripheral.wrap(mSides[ms])
print("Monitor detected on"..mSides[ms].."")
break
end
end
4 posts
Posted 08 September 2012 - 08:13 PM
This could be made much better:
mSides = {}
mSides[1] = "top"
mSides[2] = "bottom"
mSides[3] = "front"
mSides[4] = "back"
mSides[5] = "right"
mSides[6] = "left"
for ms = 1, 6 do
if peripheral.getType(mSides[ms]) == "monitor" then
mon = peripheral.wrap(mSides[ms])
print("Monitor detected on"..mSides[ms].."")
break
end
end
Thnaks I didn't think about putting the sides into an array and then looping it. I'm still not very familiar with lua.
edit : I copied this code over my original I gave you credit but if you want me to take it down tell me.
318 posts
Location
Somewhere on the planet called earth
Posted 08 September 2012 - 08:22 PM
Feel free to use it. :D/>/>
8543 posts
Posted 09 September 2012 - 03:49 AM
Or better yet,
for _,side in ipairs(rs.getSides()) do
if peripheral.getType(side) == "monitor" then
mon = peripheral.wrap(side)
break
end
end
Though, if it's actually going to be used as an API, this would be the way to use it:
function getMonitor()
local mon = nil
for _,side in ipairs(rs.getSides()) do
if peripheral.getType(side) == "monitor" then
mon = peripheral.wrap(side)
break
end
end
return mon
end
Then you'd call it like so:
mon = apiName.getMonitor()
Edit to add ipairs() to for loops.
Edited on 12 September 2012 - 02:28 AM
4 posts
Posted 12 September 2012 - 02:04 AM
Or better yet,
for _,side in rs.getSides() do
if peripheral.getType(side) == "monitor" then
mon = peripheral.wrap(side)
break
end
end
Though, if it's actually going to be used as an API, this would be the way to use it:
function getMonitor()
local mon = nil
for _,side in rs.getSides() do
if peripheral.getType(side) == "monitor" then
mon = peripheral.wrap(side)
break
end
end
return mon
end
Then you'd call it like so:
mon = apiName.getMonitor()
What does the _,side do?
8543 posts
Posted 12 September 2012 - 03:35 AM
rs.getSides() can be used to iterate through all six correctly-named sides of the computer. It returns an index (which we don't want, so we just use an underscore, which is a convention in lua for thrown-away return values) and a string that is the name of the side. This means that each iteration of the for loop has a different side in the variable 'side'.
474 posts
Posted 12 September 2012 - 04:15 AM
Even shorter:
function getMonitor()
for _,s in rs.getSides() do if peripheral.getType(s) == "monitor" then break end end return s
end
This will return a side, though
8543 posts
Posted 12 September 2012 - 04:27 AM
Yours is eight lines, if you break it out. Anything can be made into a one-liner; however, it makes no sense to do so just for the sake of shorter. Yours also always returns nil. My function above returns nil only if no monitors were found.
Also, we both forgot to call ipairs() on the result of rs.getSides().
5 posts
Location
Christchurch, New Zealand
Posted 16 September 2012 - 05:55 AM
Even shorter:
function getMonitor()
for _,s in rs.getSides() do if peripheral.getType(s) == "monitor" then break end end return s
end
This will return a side, though
As Lyqyd said, this doesn't work. The 's' variable only exists inside the for loop; when the loop finishes, 's' falls out of scope and you end up returning nil instead.