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

Trying to Broadcast a command on time...

Started by Keltaith, 15 May 2012 - 09:33 PM
Keltaith #1
Posted 15 May 2012 - 11:33 PM
So, I've never used LUA, but I used to programn very, very minor stuff about ten years ago so I'm trying to give this a go.

I'm using the auto tree farm by BikerEleven found here: http://www.computerc...eefarm-program/
(I've also stolen some of his code for what I have so far)

What I am trying to do is have my computer broadcast the command "startLoging" based off of a user inputed time in secounds. I am trying to have it reload the time whenever I relog in my server or restart it so that it is fully automated. (I need a lot of wood for my dimond factory)

I dont know how to make the function that counts time. Once I have that I might be able to make the function that broadcasts (broadcast is fine as I dont have any other computers) the command. Although, I could probably still use help there…

This is what I have so far:



Thanks in advance for any help.
MysticT #2
Posted 15 May 2012 - 11:49 PM
So you want the computer to broadcast a message for some time and then stop?
If that's it, you can use timers and a loop:

local nTime = 10 -- the time (in seconds) to broadcast. You can use the loaded time in your code
local nBroadcastTime = 1 -- time between each broadcast message

local stopTimer = os.startTimer(nTime)
local broadcastTimer = os.startTimer(nBroadcastTime)
while true do
  local evt, arg = os.pullEvent("timer")
  if arg == broadcastTimer then
	rednet.broadcast("MessageHere")
	broadcastTimer = os.startTimer(nBroadcastTime)
  elseif arg == stopTimer then
	break -- break the loop and end the broadcasting
  end
end
Keltaith #3
Posted 16 May 2012 - 07:32 AM
Thanks a lot man.

Idealy I would like it to count up and run indefinatly until it receives a stop from the user…

I modified what you put down, but I don't know if I can have two local events in the same loop or if I did it correctly…

[php]local broadcastTimer = os.startTimer(broadcasttime)
while true do
local evt, arg = os.pullEvent("timer")
local event, p1 = os.pullEvent()
if arg == broadcastTimer then
rednet.broadcast("startLoging")
broadcastTimer = os.startTimer(broadcasttime)
elseif event == "char" and p1 == "s" then
break
end
end[/php]

I'll give this a go tomorrow if no one screams moron at me (when it is not so late)
MysticT #4
Posted 16 May 2012 - 05:53 PM
You need to change the os.pullEvent("timer") to os.pullEvent(), and then check for the events:

local nTime = 10 -- the time (in seconds) to broadcast. You can use the loaded time in your code
local nBroadcastTime = 1 -- time between each broadcast message

local stopTimer = os.startTimer(nTime)
local broadcastTimer = os.startTimer(nBroadcastTime)
while true do
  local evt, arg = os.pullEvent()
  if evt == "timer" then
    if arg == broadcastTimer then
      rednet.broadcast("MessageHere")
      broadcastTimer = os.startTimer(nBroadcastTime)
    elseif arg == stopTimer then
      break -- break the loop and end the broadcasting
    end
  elseif evt == "char" then
    if arg == "s" then
	  break
    end
  end
end
Keltaith #5
Posted 17 May 2012 - 02:18 AM
Worked out a few bugs in my code. The program is working pretty well as is with the exception of one error which I have spent about 5 hours trying to figure out… :P/>/>

When restart the program it returns an "attempting to call nil error" I'm 90% sure this is in the local function loadtime() portion of my code… So to restart the program I now have to open up my server ftp and delete the timesettings.txt file under the computer id, then restart the program.

http://pastebin.com/KN7HY1Mt

I appreciate all the help you've been giving me.
Luanub #6
Posted 17 May 2012 - 02:27 AM
I dont think you can load functions as local like that. I've always received errors when trying. Try to remove the local and see if it works.
MysticT #7
Posted 17 May 2012 - 02:55 PM
I dont think you can load functions as local like that. I've always received errors when trying. Try to remove the local and see if it works.
Functions are also variables (of type "function"), so you can (and, almost allways, should) declare them local. It's like using local variables instead of globals.

@Keltaith: The problem is the stopTimer() call, that function doesn't exist, you define it later in the code and not as a function (it's a timer). So that's the attempt to call nil.
Keltaith #8
Posted 17 May 2012 - 09:50 PM
Thanks man, Ill look into it later, unfortunately I play on hardcore survival and I died last night so it might be a few days before I'm back to that point.