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

Simple rednet opener

Started by ha966, 08 October 2012 - 01:03 PM
ha966 #1
Posted 08 October 2012 - 03:03 PM
Hello,

I maded an simple script

For open your modem to the rednet

Code: pastebin get s2iV8xug rnet

Type in: rnet and your done!

Notice place the modem on the left side
MysticT #2
Posted 08 October 2012 - 03:30 PM
Why do you post this? Don't want to be mean or anything, but it's just two lines of code. You could have made it at least detect for a modem.
BigSHinyToys #3
Posted 08 October 2012 - 03:34 PM
and what about if it is not on the left side ?

this is better

local function openNet()
local listOfSides = rs.getSides()
for i = 1,6 do
  if peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem" then
   rednet.open(listOfSides[i])
   return listOfSides[i]
  end
end
return false
end
local side = openNet()
if side then
print("rednet Open "..side)
else
print("no modem")
end
ha966 #4
Posted 08 October 2012 - 03:37 PM
Whats the code of the dectector this is my first script…
BigSHinyToys #5
Posted 08 October 2012 - 07:29 PM
I will explain line by line

local function openNet() -- this defines a function
local listOfSides = rs.getSides() -- this gets a list of the sides the computer has top bottom back front left right
for i = 1,6 do -- this creats a loop that will run 6 times eatch loop i will increase by one
  if peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem" then -- if there is a peripheral and it is a modem then
   rednet.open(listOfSides[i]) -- open rednet on the side that has a modem
   return listOfSides[i] -- return the side this is what the openNet friction reruns
  end -- end of the IF statment
end -- end of the for i = loop
return false -- if it has checked all 6 sides and not returned then return false
end -- end of the openNet function
local side = openNet() -- this calls our function and the returned information goes into a variable called side
if side then -- if side is not false then
print("rednet Open "..side) -- print the side name we opened
else -- else if side is false then
print("no modem") -- print we have found no modem
end -- end of second IF statement

I will be happy to answer questions if you have any.
Rangicus #6
Posted 10 October 2012 - 12:37 AM
I just put this in the os startup script:
if peripheral.getType("left") == "modem" then
rednet.open("left")
elseif peripheral.getType("right") == "modem" then
rednet.open("right")
elseif peripheral.getType("top") == "modem" then
rednet.open("top")
elseif peripheral.getType("bottom") == "modem" then
rednet.open("bottom")
end
And that makes it so every turtle/comp that turns on checks for modems and turns them on.
PixelToast #7
Posted 10 October 2012 - 01:53 AM
I just put this in the os startup script:
if peripheral.getType("left") == "modem" then
rednet.open("left")
elseif peripheral.getType("right") == "modem" then
rednet.open("right")
elseif peripheral.getType("top") == "modem" then
rednet.open("top")
elseif peripheral.getType("bottom") == "modem" then
rednet.open("bottom")
end
And that makes it so every turtle/comp that turns on checks for modems and turns them on.
what about the back or front?
BigSHinyToys #8
Posted 12 October 2012 - 01:32 AM
I just put this in the os startup script:
if peripheral.getType("left") == "modem" then
rednet.open("left")
elseif peripheral.getType("right") == "modem" then
rednet.open("right")
elseif peripheral.getType("top") == "modem" then
rednet.open("top")
elseif peripheral.getType("bottom") == "modem" then
rednet.open("bottom")
end
And that makes it so every turtle/comp that turns on checks for modems and turns them on.
what about the back or front?

and if you have more than one it will broadcast X times the signal. for example if you have three connected you will send the same signal three times.
MysticT #9
Posted 12 October 2012 - 01:56 AM
and if you have more than one it will broadcast X times the signal. for example if you have three connected you will send the same signal three times.
Says who? The code would open only one modem. And even if there where more than one open it would send it just once.
BigSHinyToys #10
Posted 12 October 2012 - 05:48 PM
Says who? The code would open only one modem. And even if there where more than one open it would send it just once.

thanks for reminding me to test code before criticizing.

My apologizes I now understand it would only open one side.
Pinkishu #11
Posted 12 October 2012 - 07:11 PM
I'm just gonna own all of you right now with this code:

term.clear()
term.setCursorPos(1,1)
for n,m in ipairs(rs.getSides()) do
	term.setTextColor(colors.yellow)
	write("Opening modem: ")
	term.setTextColor(colors.red)
	write(m)
	term.setTextColor(colors.green)
	write(".")
	rednet.open(m)
end
term.setTextColor(colors.white)
Happy understanding! This only works in Version 1.45 PR1 of ComputerCraft

ew ipairs :)/>/>
shortened version without ipairs:
for n,m in pairs(rs.getSides()) do rednet.open(m) end
billysback #12
Posted 12 October 2012 - 07:23 PM
Happy understanding! This only works in Version 1.45 PR1 of ComputerCraft

you added in your own emoticon just for that post…
(super off topic, but I just had to say it… why?)
ficolas #13
Posted 16 October 2012 - 06:50 PM
Why to upload ur first program?
My firt program said hello world and only that, if everybody who use computercraft upload his first program there will be so many hello world and other useless crap programs
CallMeGavin #14
Posted 16 October 2012 - 10:34 PM
none of these are really good :l

this one detects if it's a modem:
tSides = { "top","bottom","back","left","right","front" }
for i = 1,#tSides do
  if peripheral.getType( tSide[i] ) == "modem" then
	rednet.open( tSide[i] )
  end
end

You could even do
local tSides = { "top","bottom","back","left","right","front" }
for i = 1,#tSides do
  if peripheral.getType( tSide[i] ) == "modem" and not rednet.isOpen( tSides[i] ) then
	rednet.open( tSide[i] )
  elseif peripheral.getType( tSide[i] == "modem" and rednet.isOpen( tSides[i] ) then
	print( "Modem is open on the "..tSides[i].."!" )
  else
	print( "No modem found!" )
  end
end

to be fancy :D/>/>