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

[LUA/REDNET] Not getting a message in rednet?

Started by ComputerCraftFan11, 07 March 2012 - 01:34 AM
ComputerCraftFan11 #1
Posted 07 March 2012 - 02:34 AM


homepage = "home";
website = "home";
nw = "";
idd = os.computerID()
port1 = idd * 32
port2 = 0
port3 = port2 / 32

rednet.open("right")

function home()
print("Welcome to RedNet explorer! This requires you to have a wireless modem on the right side of your computer. This is the default homepage. A homepage option might be added in 1.2")
print("Host a website at:         ")
print("www.newsite.com!           ")
print("   -ComputerCraftFan11     ")
end

function newsite()
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end

cPrint("Rednet Site Manager")
print("")
print("Welcome to the rednet site manager!")
print("To make a new website, enter a domain name (.com will be added for you.):")
end

function newsite2()
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end

cPrint("Rednet Site Manager")
print("")
print("People can connect to this site by going to www." ..nw.. ".com")
print("You can edit this site by going to www." ..nw.. ".com/editor")
print("Your port is " ..port1)
end

function loadwebpage()
id, message  = rednet.receive()

if message == "www." ..nw.. ".com" then -- If someone tries to connect, it will send back the website
rednet.send(id, "If you see this message your connected")
end

print (message)
end

function drawScreen()
term.clear()
term.setCursorPos(1,1)
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end

cPrint("Rednet Explorer 1.1")

local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end

cPrint("rdnt://"..website)

print("")

if website == "home" then
home()
elseif website == "www.newsite.com" then
newsite()
elseif website == "www.newsite.com/nsite/index.php?/" then
newsite2()
else
write("Port:") 
port = read()
port2 = port
loadwebpage()
end


print("")

if website == "www.newsite.com" then
write("www.")
nw = read()
newsite = nw
website = "www.newsite.com/nsite/index.php?/"
drawScreen()
else
write("Goto:") 
address = read()
website = address
drawScreen()
rednet.send(port3, website)
end

end

drawScreen()

It doesn't print out the message at


function loadwebpage()
id, message  = rednet.receive()
if message == "www." ..nw.. ".com" then -- If someone tries to connect, it will send back the website
  rednet.send(id, "If you see this message your connected")
end

print (message)
end
help
ComputerCraftFan11 #2
Posted 08 March 2012 - 12:45 AM
i need help
Advert #3
Posted 08 March 2012 - 12:58 AM
I'm not sure exactly what the problem is, but I find this part suspicious:

port2 = 0
port3 = port2 / 32

Port3 will always become 0. You will need to calculate Port3 every time you change port2.

You also don't need to re-defined cPrint every time you make a function, you can just put it above these functions, and they will all be able to use it:

local function cPrint() end
function functionThatUsescPrint() end
function otherFunctionThatUsescPrint() end
...

And, there's this:


...

port = read()
port2 = port
loadwebpage()
...

At loadwebpage:


id, message  = rednet.receive()
if message == "www." ..nw.. ".com" then -- If someone tries to connect, it will send back the website
rednet.send(id, "If you see this message your connected")


It looks like the computer will just freeze trying to recieve rednet data when you do that.

In addition to that, I'd reccomend not using all these global variables, since you don't really need them:



homepage = "home";
website = "home";
nw = "";
idd = os.computerID()
port1 = idd * 32
port2 = 0
port3 = port2 / 32

You can change it to:


local homepage = "home";
local website = "home";
local nw = "";
...
ComputerCraftFan11 #4
Posted 08 March 2012 - 01:10 AM
I'm not sure exactly what the problem is, but I find this part suspicious:

port2 = 0
port3 = port2 / 32
[spoiler]

[s]
Port3 will always become 0. You will need to calculate Port3 every time you change port2.

You also don't need to re-defined cPrint every time you make a function, you can just put it above these functions, and they will all be able to use it:

local function cPrint() end
function functionThatUsescPrint() end
function otherFunctionThatUsescPrint() end
...

And, there's this:


...

port = read()
port2 = port
loadwebpage()
...

At loadwebpage:


id, message  = rednet.receive()
if message == "www." ..nw.. ".com" then -- If someone tries to connect, it will send back the website
rednet.send(id, "If you see this message your connected")


It looks like the computer will just freeze trying to recieve rednet data when you do that.

In addition to that, I'd reccomend not using all these global variables, since you don't really need them:



homepage = "home";
website = "home";
nw = "";
idd = os.computerID()
port1 = idd * 32
port2 = 0
port3 = port2 / 32

You can change it to:


local homepage = "home";
local website = "home";
local nw = "";
...

I changed the code to:

local homepage = "home";
local website = "home";
local nw = "";
local idd = os.computerID()
local port1 = idd * 32
local port2 = 0
local port3 = port2 / 32
rednet.open("right")
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end
function home()
print("Welcome to RedNet explorer! This requires you to have a wireless modem on the right side of your computer. This is the default homepage. A homepage option might be added in 1.2")
print("Host a website at:		 ")
print("www.newsite.com!		   ")
print("   -ComputerCraftFan11	 ")
end
function newsite()
cPrint("Rednet Site Manager")
print("")
print("Welcome to the rednet site manager!")
print("To make a new website, enter a domain name (.com will be added for you.):")
end
function newsite2()
cPrint("Rednet Site Manager")
print("")
print("People can connect to this site by going to www." ..nw.. ".com")
print("You can edit this site by going to www." ..nw.. ".com/editor")
print("Your port is " ..port1)
end
function loadwebpage()
id, message  = rednet.receive()
if message == "www." ..nw.. ".com" then -- If someone tries to connect, it will send back the website
  rednet.send(id, "If you see this message your connected")
end

print (message)
end
function drawScreen()
term.clear()
term.setCursorPos(1,1)
cPrint("Rednet Explorer 1.1")

cPrint("rdnt://"..website)
print("")

if website == "home" then
  home()
elseif website == "www.newsite.com" then
  newsite()
  print("")
  write("www.")
  nw = read()
  newsite = nw
  website = "www.newsite.com/nsite/index.php?/"
  drawScreen()
elseif website == "www.newsite.com/nsite/index.php?/" then
  newsite2()
else
  print("")
  write("Goto:")
  address = read()
  website = address
  rednet.send(port3, website)
  write("Port:")
  port = read()
  port2 = port
  port3 = port2 / 32
  drawScreen()
  loadwebpage()
end
end
drawScreen()
And it still freezes at:

id, message  = rednet.receive()
Because I know that when you do this, it stops the program and stuff until you get a message.
So its getting a problem somewhere at:

  rednet.send(port3, website)

EDIT:
At

  print("")
  write("Goto:")
  address = read()
  website = address
  rednet.send(port3, website)
  write("Port:")
  port = read()
  port2 = port
  port3 = port2 / 32
  drawScreen()
  loadwebpage()
I changed it to:

  print("")
  write("Goto:")
  address = read()
  website = address
  write("Port:")
  port = read()
  port2 = port
  port3 = port2 / 32
  rednet.send(port3, website)
  drawScreen()
  loadwebpage()
But it still doesn't work

Oops, i broke it
New code:

local homepage = "home";
local website = "home";
local nw = "";
local idd = os.computerID()
local port1 = idd * 32
local port2 = 0
local port3 = port2 / 32
rednet.open("right")
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end
function home()
print("Welcome to RedNet explorer! This requires you to have a wireless modem on the right side of your computer. This is the default homepage. A homepage option might be added in 1.2")
print("Host a website at:		 ")
print("www.newsite.com!		   ")
print("   -ComputerCraftFan11	 ")
end
function newsite()
cPrint("Rednet Site Manager")
print("")
print("Welcome to the rednet site manager!")
print("To make a new website, enter a domain name (.com will be added for you.):")
end
function newsite2()
cPrint("Rednet Site Manager")
print("")
print("People can connect to this site by going to www." ..nw.. ".com")
print("You can edit this site by going to www." ..nw.. ".com/editor")
print("Your port is " ..port1)
end
function loadwebpage()
id, message  = rednet.receive()
if message == "www." ..nw.. ".com" then -- If someone tries to connect, it will send back the website
  rednet.send(id, "If you see this message your connected")
end

print (message)
end
function drawScreen()
term.clear()
term.setCursorPos(1,1)
cPrint("Rednet Explorer 1.1")

cPrint("rdnt://"..website)
print("")

if website == "home" then
  home()
elseif website == "www.newsite.com" then
  newsite()
elseif website == "www.newsite.com/nsite/index.php?/" then
  newsite2()
else
  write("Port:")
  port = read()
  port2 = port
  port3 = port2 / 32
  rednet.send(port3, website)
  loadwebpage()
end


print("")

if website == "www.newsite.com" then
  write("www.")
  nw = read()
  newsite = nw
  website = "www.newsite.com/nsite/index.php?/"
  drawScreen()
else
  write("Goto:")
  address = read()
  website = address
  drawScreen()
end
end
drawScreen()