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

[LUA] [QUESTION] Code not doing what I think.....

Started by Wing, 21 July 2012 - 11:46 PM
Wing #1
Posted 22 July 2012 - 01:46 AM
Hey,
I recently started using computercraft and have run in to a problem. I've been try to print out messages in sequence with a rednet.receive() as the trigger and many varriables but can't get it to work. Hoping someone can help!



function log()
rednet.open("back")
message = rednet.receive(1)
local theatlog = { 65, 66, 62, 64, 63, 61 }
local nCurrentIndexPos = 0
while true do
sleep(1)
if rednet.receive() then
if message == theatlog[nCurrentIndexPos] then
nCurrentIndexPos = nCurrentIndexPos + 1
if nCurrentIndexPos  == 1 then
print("Reactor 1 Stable")
elseif nCurrentIndexPos == 2 then
print("Reactor 2 Stable")
elseif nCurrentIndexPos == 3 then
print("Reactor 3 Stable")
elseif nCurrentIndexPos == 4 then
print("Reactor 4 Stable")
elseif nCurrentIndexPos == 5 then
print("Reactor 5 Stable")
elseif nCurrentIndexPos == 6 then
print("Breeder Reactor Stable")
end
if nCurrentIndexPos > 6 then
nCurrentIndexPos = 0
end
end
end
end
end
log()
That was the receiving terminal which would print the messages and the next is 1 of 6 sending information.


function heat()
rednet.open("left")
while true do
a = rs.getInput("front")
sleep(3)
if a == true then
rednet.send(60, "|Reactor 2 is critical")  -- In the first program this is "66" because of it's computer ID, so really this line isn't really needed right?
else
rednet.send(60, "|Reactor 2 is offline/cold")
end
end
end
heat()

Please help, my reactors need it! :)/>/>
Grim Reaper #2
Posted 22 July 2012 - 08:30 AM
If you could specify any of the problems you are having, that would be great! :)/>/>
Noodle #3
Posted 23 July 2012 - 11:29 AM

while true do
 sleep(0)
 id, msg = rednet.receive()
 if msg == "blah"
 -- All of code here
 end
end
1: You should make it sleep 0 or .1
2: You need to specify the vars in the rednet.receive() (I.E. id, msg = rednet.receive() if msg == "blah" then – Whatever
Pinkishu #4
Posted 23 July 2012 - 11:37 AM

while true do
sleep(0)
id, msg = rednet.receive()
if msg == "blah"
-- All of code here
end
end
1: You should make it sleep 0 or .1
2: You need to specify the vars in the rednet.receive() (I.E. id, msg = rednet.receive() if msg == "blah" then – Whatever

why make it sleep at all o.o
Noodle #5
Posted 23 July 2012 - 11:39 AM
^ Timeout..
It'll keep looping until something happens, It'll timeout and give an error.. Was added to CC
Pinkishu #6
Posted 23 July 2012 - 11:46 AM


function receive( nTimeout )
local timer = nil
if nTimeout then
timer = os.startTimer( nTimeout )
end
while true do
local e, p1, p2, p3 = os.pullEvent()
if e == "rednet_message" then
return p1, p2, p3
elseif e == "timer" and p1 == timer then
return nil, nil, nil
end
end
end

it waits for event though :)/>/>
Noodle #7
Posted 23 July 2012 - 11:51 AM
That code does, yes. But it's not in HIS/HER code. I'm manipulating the code, not adding.
Pinkishu #8
Posted 23 July 2012 - 11:51 AM
thats from the rednet API
Noodle #9
Posted 23 July 2012 - 11:53 AM
Hmm, What if you are sending multiple messages? It'll timeout without the sleep.
Pinkishu #10
Posted 23 July 2012 - 11:53 AM
Well sleep() doesn't do much more but starting a timer and waiting for the timer event xD
waiting for an event yields
thus it should not timeout

Also sleep() would eat messages
Noodle #11
Posted 23 July 2012 - 11:54 AM
Not sleep(0) because it sleeps nothing. Its just a failsafe.
Pinkishu #12
Posted 23 July 2012 - 11:56 AM
function sleep( _nTime )
    local timer = os.startTimer( _nTime )
repeat
local sEvent, param = os.pullEvent( "timer" )
until param == timer
end

well it should yield
Noodle #13
Posted 23 July 2012 - 11:57 AM
It should unless its being over-loaded with messages. Send 10 messages at once, it'll timeout. Again, JUST A FAILSAFE.
Pinkishu #14
Posted 23 July 2012 - 12:00 PM
And sleep eats such messages because it calls pullEvent :)/>/>

So lets reiterate

Sleep makes a timer
timer fires and is added at the end of the event queue
All queue'd messages (rednet_message events) are cleared that came in before sleep() because it drops everything till it hits the timer event

Rednet waits for a rednet_message event (yielding in the process) and doesn't eat messages
Noodle #15
Posted 23 July 2012 - 12:03 PM
sleep(0) is a failsafe -.-
It works so the timeout isn't hit and so it doesn't overload it. It won't crash WHAT-SO-EVER unless acted upon by some human (CTRL + T).
Also, Sleep(0) makes it so it'll always receive messages since the time that it stops doesn't exist.
Pinkishu #16
Posted 23 July 2012 - 12:17 PM
So lets see
10 messages arrive

Event Queue:
rednet_message MSG1
rednet_message MSG2
rednet_message MSG3
rednet_message MSG4
rednet_message MSG5
rednet_message MSG6
rednet_message MSG7
rednet_message MSG8
rednet_message MSG9
rednet_message MSG10

since rednet.receive waits for a rednet_message event (and yielded)
it gets that event passed in and processes it, event queue so far:

rednet_message MSG2
rednet_message MSG3
rednet_message MSG4
rednet_message MSG5
rednet_message MSG6
rednet_message MSG7
rednet_message MSG8
rednet_message MSG9
rednet_message MSG10

Now we call sleep, sleep adds a timer which fires immediately. Event queue:


rednet_message MSG2
rednet_message MSG3
rednet_message MSG4
rednet_message MSG5
rednet_message MSG6
rednet_message MSG7
rednet_message MSG8
rednet_message MSG9
rednet_message MSG10
timer

Now it will keep pulling events till it finds the timer event it started

some
rednet_message MSG8
rednet_message MSG9
rednet_message MSG10
timer

and more


timer

It gets the timer event it wanted and quits

Event Queue:


oh noes where did the messages go?


easy test:


for i=1,10,1 do
  os.queueEvent("i"..i)
end

--sleep(0)
while true do
  local ev,p1 = os.pullEvent()
  print(ev)
end
vs.


for i=1,10,1 do
  os.queueEvent("i"..i)
end

sleep(0)
while true do
  local ev,p1 = os.pullEvent()
  print(ev)
end
Cloudy #17
Posted 23 July 2012 - 05:24 PM
You know, I'll just step right in and say that sleep(0) will disregard any rednet messages that are queued, or are queued until the sleep ends. Examine the parallel code and the sleep code if you don't believe me.
Lyqyd #18
Posted 23 July 2012 - 06:47 PM
You know, I'll just step right in and say that sleep(0) will disregard any rednet messages that are queued, or are queued until the sleep ends. Examine the parallel code and the sleep code if you don't believe me.

Yeah, I'm really not sure why anyone would listen to Noodle anyway.
Noodle #19
Posted 24 July 2012 - 03:19 AM
@Lyqyd
Thanks, I'm sure the admins will love to see this message!
BTW cyberbullying is illegal where I live. (Just a fair warning).
Wing #20
Posted 24 July 2012 - 05:20 AM
If you could specify any of the problems you are having, that would be great! :)/>/>
lol this thread got interesting!
Anyway, my problem is that nothing gets printed, therefore code isn't doing what I was expecting it to do.
Expected: Print the messages received by the reactor temp monitors in the right order, 1, 2, 3, 4, 5, Breeder.
/
not working |

Anyway guys, keep at it, 12:19am so… I'm going to sleep(), lollollol
Lyqyd #21
Posted 24 July 2012 - 05:56 AM
@Lyqyd
Thanks, I'm sure the admins will love to see this message!
BTW cyberbullying is illegal where I live. (Just a fair warning).

I doubt the admins care about someone pointing out that another poster's posts are usually unhelpful. I'm curious as to why the legality of cyberbullying where you live would be relevant. I suspect it's because you think that my post would qualify. I'd love to hear why you think that it would meet the definition.

If you could specify any of the problems you are having, that would be great! :)/>/>
lol this thread got interesting!
Anyway, my problem is that nothing gets printed, therefore code isn't doing what I was expecting it to do.
Expected: Print the messages received by the reactor temp monitors in the right order, 1, 2, 3, 4, 5, Breeder.
/
not working |

Anyway guys, keep at it, 12:19am so… I'm going to sleep(), lollollol

What were the rednet messages sent to the computer running the program?
Cloudy #22
Posted 24 July 2012 - 11:05 AM
@Lyqyd
Thanks, I'm sure the admins will love to see this message!
BTW cyberbullying is illegal where I live. (Just a fair warning).

If you think that one little post counts as cyber bullying you really have a thin skin (and I am an admin, and have seen this message).

In future, please do not post incorrect information. All the tools were there for you to find the correct information if you just bothered to read the code. You failIng to do that has derailed the thread.