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

[help] RockNet Os Listening to rednet while viewing menu.

Started by SkyRamon, 05 February 2014 - 09:00 AM
SkyRamon #1
Posted 05 February 2014 - 10:00 AM

well i am trying to make a select menu and at the same time it listens for Messages that are equal to Online? then it has to send back some data….

but the listener doesnt work anyone know why ?? i already have seen something with parralell and stuff but i want it so you can just select stuff and Select some options on the menu. anyone can explain it to me and post the code that is correct

– Rocknet Os is created by Ramon Robben.
local termWidth, termHeight = term.getSize()
local selectedItem = 1
local onMainMenu = true

term.clear()
term.setCursorPos(1,1)
print("|————————————————|")
print("|————RockNet Internet Systems————|")
print("|———-Rocket Net Security Systems———–|")
print("|———————–||———————–|")
sleep(3)
term.clear()
term.setCursorPos(1,1)
io.write("RockNet")

function listen()
while true do
id, msg, distance = rednet.receive()
FUNCTION_THAT_RUNS_STUFF(id, msg, distance)
sleep(1)
ipv4 = os.getComputerID()
if msg = "Online?" then
sleep(1)
rednet.send(id, ipv4.." Is online!")
end
end

function Choice1()
term.clear()
term.setCursorPos(1,1)
print("Who`s Online")
sleep(1)
shell.run("WhosOnline")
end

function Choice2()
term.clear()
term.setCursorPos(1,1)
print("Send Mail")
sleep(1)
shell.run("SendMail")
end

function Choice3()
term.clear()
term.setCursorPos(1,1)
print("Read Mail")
sleep(1)
shell.run("ReadMail")
end

function Exit()
onMainMenu = false
end

mainMenu = {
[1] = { text = "Who`s Online", handler = Choice1 },
[2] = { text = "Send Mail", handler = Choice2 },
[3] = { text = "ReadMail", handler = Choice3 },
[4] = { text = "Exit", handler = Exit }
}

function printMenu( menu )
for i=1,#menu do
if i == selectedItem then
print(">> "..menu.text)
else
print(" "..menu.text)
end
end
end

function onKeyPressed( key, menu )
if key == keys.enter then
onItemSelected(menu)
elseif key == keys.up then
if selectedItem > 1 then
selectedItem = selectedItem - 1
end
elseif key == keys.down then
if selectedItem < #menu then
selectedItem = selectedItem + 1
end
end
end

function onItemSelected( menu )
menu[selectedItem].handler()
end

function main()
while onMainMenu do
term.clear()
term.setCursorPos(1,1)
printMenu(mainMenu)
event, key = os.pullEvent("key")
onKeyPressed(key,mainMenu)
end
end

listen()
main()
Any help is much apreciated

i think i made serveral mistakes with the Listen Code…
Lyqyd #2
Posted 05 February 2014 - 11:12 AM
Moved to Ask a Pro.
CometWolf #3
Posted 05 February 2014 - 11:50 AM
Please use code tags!


I'd be suprised if this code worked at all. See my comments.

function listen()
while true do
id, msg, distance = rednet.receive()
FUNCTION_THAT_RUNS_STUFF(id, msg, distance)
sleep(1) --why is this even here?
ipv4 = os.getComputerID()
if msg = "Online?" then
sleep(1) -- ditto
rednet.send(id, ipv4.." Is online!")
end
end -- Missing an end here...


As for the actual functionailty you're looking for, you're probably better of sticking with the parallel API at this point.
Just replace the last 2 lines with this.

parallel.waitForAny(main,listen)
Edited on 05 February 2014 - 11:06 AM
SkyRamon #4
Posted 06 February 2014 - 07:21 AM
Please use code tags!


I'd be suprised if this code worked at all. See my comments.

function listen()
while true do
id, msg, distance = rednet.receive()
FUNCTION_THAT_RUNS_STUFF(id, msg, distance)
sleep(1) --why is this even here?
ipv4 = os.getComputerID()
if msg = "Online?" then
sleep(1) -- ditto
rednet.send(id, ipv4.." Is online!")
end
end -- Missing an end here...


As for the actual functionailty you're looking for, you're probably better of sticking with the parallel API at this point.
Just replace the last 2 lines with this.

parallel.waitForAny(main,listen)

I already knew there was something with the Listener code i did already fix that but i didnt know why i couldnt select and run listener code at same time thanks for your help
SkyRamon #5
Posted 06 February 2014 - 08:18 AM
So What i want to do with the listener is when it receives a message that is equal to Online? then it would send back a message with the id of the computer and then the message is online so it would send 798 is online! but i dont receive any message my code for Sender/Listener and Receiver.
EDIT: I have found the error i fixed it shomehow but now i got a problem with the computer running the WhosOnline Code
When it is running is shows

Message: Online? Sender ID: 690 Distance: 0
Press ctrl to get back to main menu

673 Is Online!
No such program
672 Is online!
No such program

so how can i get rid of that message ??


Os Code listener is somewere at Top.


-- Rocknet Os is created by Ramon Robben.
local termWidth, termHeight = term.getSize()
local selectedItem = 1
local onMainMenu = true
term.clear()
term.setCursorPos(1,1)
print("|------------------------------------------------|")
print("|------------RockNet Internet Systems------------|")
print("|----------Rocket Net Security Systems-----------|")
print("|-----------------------||-----------------------|")
sleep(3)
term.clear()
term.setCursorPos(1,1)
io.write("RockNet")
rednet.open("back")
function listen()
rednet.open("back")
ipv4 = os.getComputerID()
term.setCursorPos(1,2)
x = 1
while x < 6 do
id, msg, distance = rednet.receive()
  if msg == "Online?" then
sleep(2)
term.clear()
term.setCursorPos(4,4)
print ("Message: "..msg.." Sender ID: "..id.." Distance: "..distance)
term.setCursorPos(4,5)
print ("Press crtl to get back to main menu")
rednet.send(id, ipv4.." Is online!")
print ()
else
shell.run("Listener")
end
end
end
function Choice1()
term.clear()
term.setCursorPos(1,1)
print("Whos Online")
sleep(1)
shell.run("WhosOnline")
end
function Choice2()
term.clear()
term.setCursorPos(1,1)
print("Send Mail")
sleep(1)
shell.run("SendMail")
end
function Choice3()
term.clear()
term.setCursorPos(1,1)
print("Read Mail")
sleep(1)
shell.run("ReadMail")
end
function Exit()
onMainMenu = false
end
mainMenu = {
[1] = { text = "Who`s Online", handler = Choice1 },
[2] = { text = "Send Mail", handler = Choice2 },
[3] = { text = "ReadMail", handler = Choice3 },
[4] = { text = "Exit", handler = Exit }
}
function printMenu( menu )
for i=1,#menu do
if i == selectedItem then
print(">> "..menu[i].text)
else
print(" "..menu[i].text)
end
end
end
function onKeyPressed( key, menu )
if key == keys.enter then
onItemSelected(menu)
elseif key == keys.up then
if selectedItem > 1 then
selectedItem = selectedItem - 1
end
elseif key == keys.down then
if selectedItem < #menu then
selectedItem = selectedItem + 1
end
end
end
function onItemSelected( menu )
menu[selectedItem].handler()
end
function main()
while onMainMenu do
term.clear()
term.setCursorPos(1,1)
printMenu(mainMenu)
event, key = os.pullEvent("key")
onKeyPressed(key,mainMenu)
end
end
parallel.waitForAny(main,listen)

And the Code for sending command and trying to get the Online messages..


rednet.open("back")
rednet.broadcast("Online?")
x = 1
while x < 6 do
id, msg, distance = rednet.receive()
print(msg)
end

Any help would be much apreciated :)/>
Edited on 06 February 2014 - 08:05 AM
CometWolf #6
Posted 06 February 2014 - 09:14 AM
You're getting those messages whenever you call shell.run() with a program that is not on your computer. As such i can only assume you don't actually have a program called Listener, WhosOnline, SendMail or ReadMail. So the question is… why did you put shell.run there, and what exactly is it you think it does?
Edited on 06 February 2014 - 08:14 AM
SkyRamon #7
Posted 06 February 2014 - 10:08 AM
You're getting those messages whenever you call shell.run() with a program that is not on your computer. As such i can only assume you don't actually have a program called Listener, WhosOnline, SendMail or ReadMail. So the question is… why did you put shell.run there, and what exactly is it you think it does?

Your right i do not have the Programs called ReadMail SendMail or Listener i do have the program WhosOnline
WhosOnline was used to be also the program Called Listener i changed the name so thanks for telling me i didnt knew this.
Lyqyd #8
Posted 06 February 2014 - 10:21 AM
Threads merged. Please stick to one thread for all questions about a given piece of code.