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

Please Help With Constant Rednet Checking

Started by PixelFox, 03 January 2015 - 01:59 AM
PixelFox #1
Posted 03 January 2015 - 02:59 AM
<p>I am making an Emailing Client/Server and There is an annoying little problem, I want a while true do loop, where it is constantly checking for rednet signals, but I want it to do things OVER that process, so, it will be constantly checking, while you can press &amp;#39;E&amp;#39; for creating an Email Message, here is my code.<br />
<br />
rednet.open(&amp;quot;back&amp;quot;)<br />
while true do<br />
-- Check rednet signals using rednet.receive or something<br />
print(&amp;quot;Type &amp;#39;E&amp;#39; to send email message&amp;quot;)<br />
local thing = read()<br />
if thing == &amp;quot;E&amp;quot; then<br />
print(&amp;quot;Email What&amp;quot;)<br />
local Word = read()<br />
rednet.send(1, Word) -- 1 is the server<br />
end<br />
term.clear()<br />
end<br />
<br />
so yeah, can anyone help me?<br />
EDIT:<br />
New Problem:<br />
I get the &amp;quot;Type &amp;#39;E&amp;#39; to send Email Message&amp;quot; thing again, when I send a message to the other client, help, please?<br />
Spoiler<br />
<br />
term.clear()<br />
term.setCursorPos(1, 1)<br />
rednet.open(&amp;quot;back&amp;quot;)<br />
print(&amp;quot;What will your name be?&amp;quot;)<br />
local Name = read()<br />
rednet.send(1, &amp;quot;/name/&amp;quot;)<br />
rednet.send(1, Name)<br />
while true do<br />
print(&amp;quot;Type &amp;#39;E&amp;#39; to send Email Message or R to exit out&amp;quot;)<br />
local e, p1 = os.pullEvent()<br />
if e == &amp;quot;key&amp;quot; and p1 == keys.e then<br />
print(&amp;quot;Email what: &amp;quot;)<br />
local Word = read()<br />
rednet.send(1, Word)<br />
elseif e == &amp;quot;rednet_message&amp;quot; then<br />
id, msg = rednet.receive()<br />
term.clear()<br />
term.setCursorPos(1, 1)<br />
print(msg)<br />
print(&amp;quot;Type any key to go back to Emails&amp;quot;)<br />
read()<br />
elseif e == &amp;quot;key&amp;quot; and p1 == keys.r then<br />
break<br />
end<br />
end<br />
rednet.close()<br />
<br />
</p>
<p>I hope this is my last problem. But, Reply doesn&amp;#39;t work…At All, if I press reply and then reply to the person, The emails will no longer go through…..if you need the code for the server, I&amp;#39;ll give you that too, but, here is the code.</p>
<p>
Spoiler</p>
<p>
</p>
<div>term.clear()</div>
<div>term.setCursorPos(1, 1)</div>
<div>rednet.open(&amp;quot;back&amp;quot;)</div>
<div>print(&amp;quot;What will your name be?&amp;quot;)</div>
<div>local Name = read()</div>
<div>rednet.send(1, &amp;quot;/name/&amp;quot;)</div>
<div>rednet.send(1, Name)</div>
<div>while true do</div>
<div>term.clear()</div>
<div>term.setCursorPos(1, 1)</div>
<div>print(&amp;quot;Type &amp;#39;E&amp;#39; to send Email Message or R to exit out&amp;quot;)</div>
<div>local e, p1, msg = os.pullEvent()</div>
<div>if e == &amp;quot;key&amp;quot; and p1 == keys.e then</div>
<div>print(&amp;quot;Email what: &amp;quot;)</div>
<div>local Word = read()</div>
<div>rednet.send(1, Word)</div>
<div>elseif e == &amp;quot;rednet_message&amp;quot; then</div>
<div>term.clear()</div>
<div>term.setCursorPos(1, 1)</div>
<div>print(msg)</div>
<div>term.setCursorPos(1, 6)</div>
<div>print(&amp;quot;------------&amp;quot;)</div>
<div>print(&amp;quot;Press &amp;#39;E&amp;#39; to go to Emails, or &amp;#39;Y&amp;#39; to reply&amp;quot;)</div>
<div>f, p2 = os.pullEvent()</div>
<div>if f == &amp;quot;key&amp;quot; and p2 == keys.y then</div>
<div>print(&amp;quot;------------&amp;quot;)</div>
<div>print(&amp;quot;What to reply?&amp;quot;)</div>
<div>local Reply = read()</div>
<div>rednet.send(1, &amp;quot;/reply/&amp;quot;)</div>
<div>rednet.send(1, Reply)</div>
<div>elseif f == &amp;quot;key&amp;quot; and p2 == keys.e then</div>
<div>term.clear()</div>
<div>term.setCursorPos(1, 1)</div>
<div>end</div>
<div>elseif e == &amp;quot;key&amp;quot; and p1 == keys.r then</div>
<div>break</div>
<div>end</div>
<div>end</div>
<div>rednet.close()</div>
<div>
</div>
<div>
</div>
<div>Any Suggestions?</div>
Edited on 03 January 2015 - 02:38 PM
KingofGamesYami #2
Posted 03 January 2015 - 03:38 AM
You want parallel. That or rewrite read() and rednet.recieve() together. Please note that functions passed to the parallel api are not called with (), thus you cannot use functions needing arguments. To get around this, you can make your own functions.


local function name()  --#this defines the function "name"
  while true do
    local thing = read()
  end
end
local function anotherFunc()
  while true do
    --#stuff
  end
end
parallel.waitForAny( name, anotherFunc )
Cycomantis #3
Posted 03 January 2015 - 03:39 AM
You will want to look into using events in place of your read.

It would look something like.

while true do
  print("Type 'E' to send email message")
  local e,p1 = os.pullEvent()
  if e == "key" and p1 == keys.e then --with the key event p1 is the key that was pressed
  --do that stuff
  elseif e == "rednet_message" then --with the rednet_message event p1 is the message
  --do your rednet stuff
  end
end
Edited on 03 January 2015 - 02:44 AM
PixelFox #4
Posted 03 January 2015 - 04:00 AM
Ah, Thank you, both of your ideas work, now I need to make my server work that way…
PixelFox #5
Posted 03 January 2015 - 04:49 AM
There is now a quite large problem, whenever I run my new client, (there are two)
if I send anything to another computer, the "Type 'E' to send Email Message or R to exit out" just comes again…
Here is the code:
Spoiler

term.clear()
term.setCursorPos(1, 1)
rednet.open("back")
print("What will your name be?")
local Name = read()
rednet.send(1, "/name/")
rednet.send(1, Name)
while true do
print("Type 'E' to send Email Message or R to exit out")
local e, p1 = os.pullEvent()
if e == "key" and p1 == keys.e then
print("Email what: ")
local Word = read()
rednet.send(1, Word)
elseif e == "rednet_message" then
id, msg = rednet.receive()
term.clear()
term.setCursorPos(1, 1)
print(msg)
print("Type any key to go back to Emails")
read()
elseif e == "key" and p1 == keys.r then
break
end
end
rednet.close()
Pls Help!
Cycomantis #6
Posted 03 January 2015 - 05:04 AM
Change

elseif e == "rednet_message" then
id, msg = rednet.receive()
term.clear()
term.setCursorPos(1, 1)
print(msg)


To this

elseif e == "rednet_message" then
term.clear()
term.setCursorPos(1, 1)
print(p1)

You do not need the rednet.receive() the os.pullEvent() already received the message.
Edited on 03 January 2015 - 04:05 AM
PixelFox #7
Posted 03 January 2015 - 02:32 PM
Ah, OK, so that is like asking a question twice then using the answers, thank you, I'm pretty new to "OS" api.
But, uh…the messsage doesn't display, the id does, but, I can fix that, just add a new var to os.pullEvent()
Edited on 03 January 2015 - 01:35 PM
Cycomantis #8
Posted 04 January 2015 - 12:11 AM
os.pullEvent() is one of the handiest functions available to you. Here is a good tutorial you should take a look at to help familiarize yourself with it a little more.

os.pullEvent()