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

[Lua] rednet help (Rednet Explorer)

Started by ComputerCraftFan11, 05 March 2012 - 03:03 AM
ComputerCraftFan11 #1
Posted 05 March 2012 - 04:03 AM
I'm trying to create internet for computercraft but my computer freezes when i run the program.

Here is the code:

homepage = "home";
website = "home";
nw = "";
rednet.open("right")
id, message  = rednet.receive(10)
if message == "www." ..nw.. ".com" then -- If someone tries to connect, it will send back the website
rednet.send(id, "site")
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()
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")
end
function loadwebpage() -- gonna change this when the bug is fixed
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
  loadwebpage()
end


print("")

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

drawScreen()
end
drawScreen()

I don't get a error and I dont know whats wrong. Please help
ComputerCraftFan11 #2
Posted 05 March 2012 - 06:00 AM
Is it a problem in rednet?
ComputerCraftFan11 #3
Posted 06 March 2012 - 01:36 AM
Anyone? :unsure:/>/>
Espen #4
Posted 06 March 2012 - 01:54 AM
The function drawScreen() calls itself at the end, which results in infinite calls to drawScreen().
Since you're neither changing anything in that function by reading any user input or events, it will run instantly through the whole function without any noticeable delay whatsoever, over and over and over.
Btw. you've defined the nested function cPrint() two times, which is redundant. You only need to define it once and then afterwards you can call it as many times as you like.
ComputerCraftFan11 #5
Posted 06 March 2012 - 01:56 AM
The function drawScreen() calls itself at the end, which results in infinite calls to drawScreen().
Since you're neither changing anything in that function by reading any user input or events, it will run instantly through the whole function without any noticeable delay whatsoever, over and over and over.
Btw. you've defined the nested function cPrint() two times, which is redundant. You only need to define it once and then afterwards you can call it as many times as you like.

Do I remove drawScreen() at the end or do i move it? It wont work if i remove it

EDIT:
I changed the code to this:

homepage = "home";
website = "home";
nw = "";
rednet.open("right")
id, message  = rednet.receive(10)
if message == "www." ..nw.. ".com" then -- If someone tries to connect, it will send back the website
rednet.send(id, "site")
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()
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")
end
function loadwebpage()
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
  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
  rednet.send(5, website)
  drawScreen()
end

end
drawScreen()

But it still freezes the computer
ComputerCraftFan11 #6
Posted 06 March 2012 - 02:01 AM
nvm it just takes a while to turn on

i made a new website on computer 5 (this is gonna change later) and when i connect it doesnt display "site"

its suppose to send a message to that person and if its there site it will send back some information
Espen #7
Posted 06 March 2012 - 02:04 AM
Man, I really love the live-update function of this forum software.
I was about to write a pretty big post, but then I was made aware of your new post, hehe.
Saved me a lot of sweat. Glad it works for you now. :unsure:/>/>
ComputerCraftFan11 #8
Posted 06 March 2012 - 02:06 AM
Man, I really love the live-update function of this forum software.
I was about to write a pretty big post, but then I was made aware of your new post, hehe.
Saved me a lot of sweat. Glad it works for you now. :unsure:/>/>
now im having problems with rednet, i just updated my previous post with:

i made a new website on computer 5 (this is gonna change later) and when i connect it doesnt display "site"

its suppose to send a message to that person and if its there site it will send back some information

EDIT:

I noticed i put
id, message = rednet.receive(10)
so i removed the "10" but i noticed 2 things:
It took a while to open because it only opened AFTER it received a message (so it takes 10 seconds to load)
It no longer opens
ComputerCraftFan11 #9
Posted 06 March 2012 - 02:45 AM
I changed load webpage to this:


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, "site")
end

print (message)
end
and it works not but the screen doesnt load