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

Parallels And Rs Not Responding As Excpected And Sleep As Long Timer

Started by Andale, 23 January 2013 - 11:15 AM
Andale #1
Posted 23 January 2013 - 12:15 PM
I haven't quite been able to understand the parallel functions and I've started a new project that they'd be nice for but in other programs I've yet to have them work as I'd expect. Let's say I have about a dozen command blocks and want each to be activated at a certain time which is set by our server rebooting every 4 hours. Do I just need to adjust my program to run them in a row or can I set each one's timer and have them all running individually from the same computer's startup or would it be 'easier' (relatively) to use a computer for each timer instead?
In my single comp I'd just have the time's set from server startup but I don't think all the timers can run at once with parallel.

2nd issue: Can you see a difference as to why the second one below does not work from the same computer


3rdly: Is there something I should use rather than the sleep function for very long times of 15 minutes or more?



lottery: [works]

--test for magenta
rs.setBundledOutput ("back",colors.magenta)
sleep(0.1)
rs.setBundledOutput("back",0)



startup: [doesn't]
local function clear()
term.clear()
term.setCursorPos(1,1)
end
clear()

local function reset()   -- RESET for all outputs
rs.setBundledOutput("back",0)
rs.setBundledOutput("left",0)
rs.setBundledOutput("right",0)
sleep(0.2)
end
reset()

print("The command block functions timer")


-- Lottery Timer
-- 10 minutes after every server restart 'give at random'
-- location is 'back - magenta'
rs.setBundledOutput("back", colors.magenta)
reset()






-- TEST ZONE-----------------------------------------------

rs.setBundledOutput("back", colors.lime)
rs.setBundledOutput("back", colors.red)
rs.setBundledOutput("back", colors.magenta)
reset()
byebyeu2 #2
Posted 23 January 2013 - 12:18 PM
im sorry i cant answer you question but i was woundering how do you create a new topic
theoriginalbit #3
Posted 23 January 2013 - 12:23 PM
maybe try using pull events with timers, you can have individual timers but one control loop… basic example:


local firstTimer = os.startTimer( 10 )
local secondTimer = os.startTimer( 9 )
while true do
  local event, p1 = os.pullEvent()
  if event == "timer" and p1 == firstTimer then
	-- do something
	firstTimer = os.startTimer( 10 )
  elseif event == "timer" and p1 == secondTimer then
	-- do something
	secondTimer = os.startTimer( 9 )
  end
this has 2 timers, one running every 9 seconds, one running every 10 seconds…

EDIT: Also why a dozen command blocks, why not just 1 that you change the command with?
Edited on 23 January 2013 - 11:24 AM
Andale #4
Posted 23 January 2013 - 03:09 PM
ok, so os timer's CAN run simultaneously. I hadn't even thought of that. Thank you very much.


EDIT: Also why a dozen command blocks, why not just 1 that you change the command with?


Because I don't think they have that interface in CC. Do they? I know you can put text onto signs.
I am hoping a version of CC in the future will have a computer/command block recipe to allow it world actions. I hope they do NOT put in world interaction without the need for the command block at least in the recipe as that would spell disaster on a server.

Going to try this now. I will edit this post if there's anything else.
Lyqyd #5
Posted 23 January 2013 - 03:12 PM
Command blocks can be wrapped as peripherals.

Also, if you're setting the output to on, then immediately calling a function that turns the output off, it will effectively never turn on.
theoriginalbit #6
Posted 23 January 2013 - 03:36 PM
ok, so os timer's CAN run simultaneously. I hadn't even thought of that. Thank you very much.
Yeh just make sure you store the returned 'id' when you start the timer so you can check its the one you want :)/>
Andale #7
Posted 23 January 2013 - 03:37 PM
I thought about the issue of timing in the reset but not in the test zone. Went out for dinner, it was the first thing I realized when I came back. The beer might have helped too. :)/>


I did not know they could be. Here I was setting up this nice building. Maybe I'll stick to it and maybe not. If I keep this it might be easier to let the other mods add/remove commands. That is if I figure out interacting with external files to save timing and color tables.


The command block peripherals are working. But chat commands activates on setcommand and runs twice.



local cmd = peripheral.wrap("right")
local function run()
cmd.runCommand()
end
local function clear()
term.clear()
term.setCursorPos(1,1)
end
clear()
print("The command block functions timer for Prot3ch")

-- Do the important stuff first!
cmd.setCommand("/gamerule doFireTick false")
run()

-- Lottery Timer
-- 10 minutes after every server restart 'give at random'


-- TEST ZONE -----------------------------------------------
cmd.setCommand("/w @p Success!")
cmd.setCommand("/give @p 1 1")

cmd.setCommand("/w @p Success!") is showing twice and does so with /say also
cmd.setCommand("/give @p 1 1") only gives 1 block tho
Andale #8
Posted 23 January 2013 - 06:42 PM
Well it turns out that the set/run Command functions are a little buggy at the moment, so back to the way I wanted anyway with wires. Looks neater.

Anyway, I threw together a tester of what you had posted and found an issue. It doesn't seem to repeat once it has fulfilled all my elseif's. I put the counter at the bottom to count pullEvents but all it has done is shown me that the program is still awaiting the pulls but the timers are not resetting in number test #7.
I tried moving the local t4 = os.startTimer(8) around to but it always has the same result of the timer refusing to reset.

local function clear()
term.clear()
term.setCursorPos(1,1)
end

local function rst()
sleep(0.1)
rs.setBundledOutput("back", 0)
sleep(0.1)
end


rst()
clear()

print("The command block functions timer for Prot3ch")

-- Lottery Timer
-- 10 minutes after every server restart 'give at random'
-- local Lottery = os.startTimer(600)


-- The IRC Chat
-- Displays every 20 minutes
-- local IRC = os.startTimer(30)

-- Testing

local t1 = os.startTimer(2)
local t2 = os.startTimer(4)
local t3 = os.startTimer(6)
local t4 = os.startTimer(8)
local t5 = os.startTimer(10)
local t6 = os.startTimer(12)
local t7 = os.startTimer(14)
local testmath = 0



--    elseif event == "timer" and p1 == ***** then
--    ****** = os.startTimer( 10 )

while true do
    local event, p1 = os.pullEvent()            -- wait for pull event, assign "timer, title"
    if event == "timer" and p1 == Lottery then        -- check which timer
--        rs.setBundledOutput("back",   COLOR)            -- lottery
--        ********** = os.startTimer( 10 )        -- timer reset (not used for lottery)
--    elseif event == "timer" and p1 == IRC then        -- check which timer
        print ("")
    elseif event == "timer" and p1 == t1 then
        rs.setBundledOutput("back",colors.white)
    elseif event == "timer" and p1 == t2 then
        rs.setBundledOutput("back", colors.orange)
    elseif event == "timer" and p1 == t3 then
        rs.setBundledOutput("back", colors.magenta)
    elseif event == "timer" and p1 == t4 then
        rs.setBundledOutput("back", colors.lightBlue)
    elseif event == "timer" and p1 == t5 then
        rs.setBundledOutput("back", colors.yellow)
    elseif event == "timer" and p1 == t6 then
        rs.setBundledOutput("back", colors.lime)
    elseif event == "timer" and p1 == t7 then
        rs.setBundledOutput("back", colors.pink)
local t1 = os.startTimer(2)
local t2 = os.startTimer(4)
local t3 = os.startTimer(6)
local t4 = os.startTimer(8)
local t5 = os.startTimer(10)
local t6 = os.startTimer(12)
local t7 = os.startTimer(14)
    end

testmath = testmath + 1
print(testmath)

end
LBPHacker #9
Posted 23 January 2013 - 09:40 PM
Try using


t1 = os.startTimer(2)
t2 = os.startTimer(4)
t3 = os.startTimer(6)
t4 = os.startTimer(8)
t5 = os.startTimer(10)
t6 = os.startTimer(12)
t7 = os.startTimer(14)

in the loop instead of


local t1 = os.startTimer(2)
local t2 = os.startTimer(4)
local t3 = os.startTimer(6)
local t4 = os.startTimer(8)
local t5 = os.startTimer(10)
local t6 = os.startTimer(12)
local t7 = os.startTimer(14)



And


if event == "timer" then
    if p1 == t1 then
        ...
    elseif ...
    end
end

is much easier than


if event == "timer" and p1 == t1 then
    ...
elseif ...
Andale #10
Posted 24 January 2013 - 04:00 AM
Very good point with the double if since i am always going to be calling on event = timer
Thank you, will make the code much cleaner

You know, i can't believe believe I made that rookie mistake of copy/pasting and leaving the local on. I'm honestly having a really hard time keeping the 'local' in mind.

I've seen people declare all their arguments/variables at the top before everything else

-- start of file
local f1, timer, boots, pizza
-- the rest after this
If I do that will it keep ALL callings to those arguments as local to the program so I can avoid putting 'local' anywhere else?
I constantly find myself putting them inside of functions or loops and screwing myself up.
ChunLing #11
Posted 24 January 2013 - 09:14 AM
Yes, the initial declaration of an identifier sets the scope for the variable. This doesn't prevent you from declaring a new variable with the same identifier in a different scope, though (and those errors can be more difficult to find, since the identifier is still associated with a variable after the scope ends, just not the same variable you were wanting to use). Ultimately, there's no replacement for being careful.