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

[Lua] Problem with repeat loops and timers

Started by civilwargeeky, 19 April 2013 - 11:39 AM
civilwargeeky #1
Posted 19 April 2013 - 01: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
Engineer #2
Posted 19 April 2013 - 01: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
civilwargeeky #3
Posted 19 April 2013 - 01:49 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
Oh I'm sorry, interval was assigned in the config. Currently interval is set to 5
civilwargeeky #4
Posted 19 April 2013 - 04:21 PM
So is this a bug or what?
Spongy141 #5
Posted 19 April 2013 - 04:39 PM
Whats with having just

local message
and not define the message? Or is there more to your code?
Bubba #6
Posted 19 April 2013 - 06:07 PM
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.
civilwargeeky #7
Posted 20 April 2013 - 02:54 AM
@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.
Doyle3694 #8
Posted 20 April 2013 - 02:58 AM
@Engineer, there are no integers in lua, nor are there doubles. There is only numbers.
Engineer #9
Posted 20 April 2013 - 04:10 AM
@Engineer, there are no integers in lua, nor are there doubles. There is only numbers.
It was for the sake of the example
Bubba #10
Posted 20 April 2013 - 04:11 AM
@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.
civilwargeeky #11
Posted 20 April 2013 - 05: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
civilwargeeky #12
Posted 20 April 2013 - 05:36 PM
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