Jump to content




[Lua] Problem with repeat loops and timers


  • You cannot reply to this topic
11 replies to this topic

#1 civilwargeeky

  • Members
  • 393 posts

Posted 19 April 2013 - 12:39 PM

Hello. In a section of my quarry rednet receiver program, there is a part where it should wait for either a message or a five second timer, however it does not ever activate for the timer. I cannot figure out what I'm doing wrong. Can anyone help?
local message
local timeWaited = 0

term.clear()
while message ~= "stop" do
repeat
local id = os.startTimer(interval)
local event, idCheck, sendChannel, _, locMessage, distance = os.pullEvent()
message = locMessage
if (event == "timer" and idCheck == id) then timeWaited = timeWaited + interval else timeWaited = 0 end
until (event == "modem_message" and idCheck == modemSide and sendChannel == sender) or (event == "timer" and idCheck == id)
sendMsg(relayMessage)
--Text to screen stuff
end


#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 19 April 2013 - 12:47 PM

That is because interval is never defined, you should have a number as integer. Eg. 1 and 4, every number that has no decimals. That is a double; 0.24

#3 civilwargeeky

  • Members
  • 393 posts

Posted 19 April 2013 - 12:49 PM

View PostEngineer, on 19 April 2013 - 12:47 PM, said:

That is because interval is never defined, you should have a number as integer. Eg. 1 and 4, every number that has no decimals. That is a double; 0.24
Oh I'm sorry, interval was assigned in the config. Currently interval is set to 5

#4 civilwargeeky

  • Members
  • 393 posts

Posted 19 April 2013 - 03:21 PM

So is this a bug or what?

#5 Spongy141

  • Members
  • 526 posts
  • Location'Merica

Posted 19 April 2013 - 03:39 PM

Whats with having just
local message
and not define the message? Or is there more to your code?

#6 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 19 April 2013 - 05:07 PM

View PostSpongy141, on 19 April 2013 - 03:39 PM, said:

Whats with having just
local message
and not define the message? Or is there more to your code?

If you define the variable without giving it a value, it will maintain the same scope throughout the entire program (or at least until you define another variable with the same name, in which case the new variable will take precedence until you have exited the function).

@OP: What do you mean it never activates the timer? Your code works fine for me.

#7 civilwargeeky

  • Members
  • 393 posts

Posted 20 April 2013 - 01:54 AM

View PostBubba, on 19 April 2013 - 05:07 PM, said:


@OP: What do you mean it never activates the timer? Your code works fine for me.
Well. When I use the full program, it will receive messages and display them to the screen, but it will not update a section that says "Seconds since last message." One thing to note is that this function is part of a coroutine. The other coroutine function just waits for read() to relay commands to the turtle.

#8 Doyle3694

  • Members
  • 815 posts

Posted 20 April 2013 - 01:58 AM

@Engineer, there are no integers in lua, nor are there doubles. There is only numbers.

#9 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 20 April 2013 - 03:10 AM

View PostDoyle3694, on 20 April 2013 - 01:58 AM, said:

@Engineer, there are no integers in lua, nor are there doubles. There is only numbers.
It was for the sake of the example

#10 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 20 April 2013 - 03:11 AM

View Postcivilwargeeky, on 20 April 2013 - 01:54 AM, said:

View PostBubba, on 19 April 2013 - 05:07 PM, said:


@OP: What do you mean it never activates the timer? Your code works fine for me.
Well. When I use the full program, it will receive messages and display them to the screen, but it will not update a section that says "Seconds since last message." One thing to note is that this function is part of a coroutine. The other coroutine function just waits for read() to relay commands to the turtle.

You should provide the whole code because the problem may lie in that; I was able to get this to run just fine. My guess is that the function that is supposed to write the time to the screen is not actually updating anything.

#11 civilwargeeky

  • Members
  • 393 posts

Posted 20 April 2013 - 04:34 PM

Ok. Here is the code. Sorry it might be a bit messy, but it was in the middle of being updated.
http://pastebin.com/6L2JZPjH
The part where it outputs time is line 84

#12 civilwargeeky

  • Members
  • 393 posts

Posted 20 April 2013 - 04:36 PM

View PostBubba, on 20 April 2013 - 03:11 AM, said:

You should provide the whole code because the problem may lie in that; I was able to get this to run just fine. My guess is that the function that is supposed to write the time to the screen is not actually updating anything.

Ok. Here is the code. Sorry it might be a bit messy, but it was in the middle of being updated.
http://pastebin.com/6L2JZPjH
The part where it outputs time is line 84

Here is the turtle program that (should) work with this version of the receiver. Use the argument "-rednet true" when you start
http://pastebin.com/UC0XehGH





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users