16 posts
Posted 09 January 2013 - 04:19 PM
Hi there!
Just a quick question, is there a way to check if a modem exists and return the parameters true/false and the side which it is on? If not, is there something out there that could help me do this?
Thank you very much for your time! :D/>
7508 posts
Location
Australia
Posted 09 January 2013 - 04:23 PM
This should do the trick… :)/>
function findModem()
for _, v in pairs( rs.getSides() ) do
if peripheral.isPresent( v ) and peripheral.getType( v ) == "modem" then
return true, v
end
end
return false, nil
end
to use this you would do this
local exist, side = findModem()
Edited on 09 January 2013 - 03:24 PM
16 posts
Posted 09 January 2013 - 04:31 PM
Great! Thanks for the quick reply! So i would place the
<code>
local exist, side = findmodem()
</code>
and If i wanted to check it existed, i would go:
<code>
if exist == true then
–code
else
print("Please place a modem on the computer!")
end
</code>
and if i was to send a rednet message, I could go:
<code>
rednet.open(side)
rednet.broadcast("Whatevs")
rednet.close(side)
</code>
That would work right? :D/>
139 posts
Location
USA
Posted 09 January 2013 - 04:34 PM
looks good
7508 posts
Location
Australia
Posted 09 January 2013 - 04:38 PM
That would work right? :D/>
yes that would work. alternatively you could just have it return the side or nil. then do this
if not side then -- nil in Lua is interpreted as false
print( "No modem" )
end
rednet.open( side )
-- ... etc
also an fyi these are the code tags ( you were close though )
[
code] [
/code]
16 posts
Posted 09 January 2013 - 04:39 PM
Just tried it out, works perfectly!
Thanks so much!! :)/>