This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
2 Programs Running At a Time
Started by Pedroc1999, 31 October 2012 - 10:18 AMPosted 31 October 2012 - 11:18 AM
As some may know i have made a computer program to work as a lever, just overly complicated and fancy, I want to know if it is possible to have my door program running but have a rednet computer assume control at any given moment, so it is running my computer program to open my doors, but I could be on another computer ordering the computer to open door or close door or just create a file and edit programs from a different computer remortly
Posted 31 October 2012 - 11:30 AM
2 separate programs is not possible as far as i know.
You could put all of the functions into one program then use the parallel API to queue up both of them.
You could put all of the functions into one program then use the parallel API to queue up both of them.
local function prog1Main()
--stuff
end
local function prog2Main()
--stuff
end
parallel.waitForAny(prog1Main, prog2Main)
Posted 31 October 2012 - 11:32 AM
how would that work as i am a bit of a noob
Posted 31 October 2012 - 11:35 AM
I've not got to doing parallel stuff yet but from what I understand (can't test this at the moment) I think its something like:
If I am correct this will loop the program forever, checking each function and stopping when the first one completes. Hopefully Luanub will correct me if I'm wrong!
local function prog1Main()
--stuff
end
local function prog2Main()
--stuff
end
while(true) do
parallel.waitForAny(prog1Main, prog2Main)
end
If I am correct this will loop the program forever, checking each function and stopping when the first one completes. Hopefully Luanub will correct me if I'm wrong!
Posted 31 October 2012 - 11:39 AM
i cant test right now but for my door to open i need to input a password, if it keeps looping at hyper speed i will have no chance of writing it it, if i put a sleep for lets say 3 seconds, as my second program is a rednet.recieve and if there is no information it will be skipped so no information on screen means it will go back to it, hasnt anyone coded a program to enable dual programs yet
Posted 31 October 2012 - 11:49 AM
I'll test it when I get home but that is not for another 6 hours or so!
From what I've read, you'd be able to input your password as the only time it would not let you complete is if the other function completed first, only once ANY of the functions complete would the loop start again.
so if function one has a read() it will wait at that point till it has something, if your second function loops around it's self till a signal is detected then which ever one happens first completes the function and the program moves on (in the example I gave starting the loop again).
From what I've read, you'd be able to input your password as the only time it would not let you complete is if the other function completed first, only once ANY of the functions complete would the loop start again.
so if function one has a read() it will wait at that point till it has something, if your second function loops around it's self till a signal is detected then which ever one happens first completes the function and the program moves on (in the example I gave starting the loop again).
Posted 31 October 2012 - 12:10 PM
Would it be possible to do two shell.run()s with the parallel API?
function prog1()
shell.run('program1')
end
function prog2()
shell.run('program2')
end
parallel.waitForAll(prog1, prog2)
Posted 31 October 2012 - 12:12 PM
Adding that to the test list! If anyone can test these now please do and let me know other wise I'll post back here later.
Posted 31 October 2012 - 12:13 PM
yeah sure but parallel isn't really parallel, it waits for a courotine yield and then runs the next program until it gets a courotine yield and then switch back etc.
Posted 31 October 2012 - 01:40 PM
so if I run my door program and my rednet command program, I would only see the door program but if someone were to send me a rednet command, the door program would stop and my rednet command would be executed(with the right code obviously
Posted 31 October 2012 - 03:27 PM
how would i set it out though?
Posted 31 October 2012 - 08:39 PM
Would it be possible to do two shell.run()s with the parallel API?function prog1() shell.run('program1') end function prog2() shell.run('program2') end parallel.waitForAll(prog1, prog2)
Possibilities of having some nesting happen there, would try to avoid that one.
Posted 31 October 2012 - 08:42 PM
so…..
Posted 31 October 2012 - 08:46 PM
Just put it into functions into one program :?
Posted 31 October 2012 - 08:46 PM
How are the triggers setup for the two programs? Personally I would just put it all in one program. Then if one is a rednet trigger and one is say a redstone trigger I would do something like:
The rest of the code for the two programs would be above this and would be called by the two functions. If you want stuff printed before the pullEvent() or rednet.receive() you can have it do that and the function will run up until the point it yields.
function one()
os.pullEvent("redstone")
--do this programs stuff
end
function two()
local id,msg = rednet.recieve()
--do this ones stuff
end
while true do
parallel.waitForAny(one, two)
end
The rest of the code for the two programs would be above this and would be called by the two functions. If you want stuff printed before the pullEvent() or rednet.receive() you can have it do that and the function will run up until the point it yields.
Edited on 31 October 2012 - 07:49 PM
Posted 31 October 2012 - 09:46 PM
thx