This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ftank12's profile picture

Check if a modem exists

Started by ftank12, 09 January 2013 - 03:19 PM
ftank12 #1
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/>
theoriginalbit #2
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
ftank12 #3
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/>
crazyguymgd #4
Posted 09 January 2013 - 04:34 PM
looks good
theoriginalbit #5
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]
ftank12 #6
Posted 09 January 2013 - 04:39 PM
Just tried it out, works perfectly!

Thanks so much!! :)/>