Jump to content




Check if a modem exists

computer lua wireless

5 replies to this topic

#1 ftank12

  • Members
  • 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

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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 by TheOriginalBIT, 09 January 2013 - 04:24 PM.


#3 ftank12

  • Members
  • 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

#4 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 09 January 2013 - 04:34 PM

looks good

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 January 2013 - 04:38 PM

View Postftank12, on 09 January 2013 - 04:31 PM, said:

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]

#6 ftank12

  • Members
  • 16 posts

Posted 09 January 2013 - 04:39 PM

Just tried it out, works perfectly!

Thanks so much!! :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users