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

is it possible to run two lines at once?

Started by mrgreaper, 16 March 2012 - 01:14 AM
mrgreaper #1
Posted 16 March 2012 - 02:14 AM
how do i make it so while one part of a program is runing it carrys on running another

for example
run

monitorapi.marqueeLeft("top", "testing……….", 3, 0.1)
and
monitorapi.marqueeLeft("top", "testing……….", 2, 0.1)

at the same time

another example would be to have a monitier that displays counters kept up to date from multiple sides

in the past i have just added the stuff i want into the code for the anti ctrl+t but im sure thats not what im ment to do and well it doesnt always work

so in short 2 questions

1) how do i make it not wait for a command to finish before it moves to the next command?

2) how do i make a loop that runs in the background?



hmmm Parrallel seems to be a possible key,

so i tried

parallel.waitForAny( one, two, three, four, five)
function one()
monitorapi.marqueeLeft("top", "testing5..........", 5, 0.1)
end
function two()
monitorapi.marqueeLeft("top", "testing4..........", 4, 0.1)
end
function three()
monitorapi.marqueeLeft("top", "testing3..........", 3, 0.1)
end
function four()
monitorapi.marqueeLeft("top", "testing2..........", 2, 0.1)
end
function five()
monitorapi.marqueeLeft("top", "testing1..........", 1, 0.1)
end
one()
two()
three()
four()
five()

program just sets there doing nothing :D/>/>

if i – the first line it works (not as i want it to but 1 line at a time)

also tried parallel.waitForAll
Edited on 16 March 2012 - 01:49 AM
Casper7526 #2
Posted 16 March 2012 - 03:01 AM
You can't ever actually run 2 pieces of code at the same time, but you can however give that illusion based on yields.


function foo()
foo1 = math.random(1,5)
sleep(foo1)
print ("I am foo 1 and I slept for "..foo1)
end


function foo2()
foo2 = math.random(1,5)
sleep(foo2)
print ("I am foo 2 and I slept for "..foo2)
end

while true do
parallel,waitForAny(foo, foo2)
end
mrgreaper #3
Posted 16 March 2012 - 03:21 AM
You can't ever actually run 2 pieces of code at the same time, but you can however give that illusion based on yields.


function foo()
foo1 = math.random(1,5)
sleep(foo1)
print ("I am foo 1 and I slept for "..foo1)
end


function foo2()
foo2 = math.random(1,5)
sleep(foo2)
print ("I am foo 2 and I slept for "..foo2)
end

while true do
parallel,waitForAny(foo, foo2)
end

gave it a test to get my head around it (changing a couple of the lines to make it work) will be useful with some of my ideas, others not so, shame it cant run stuff in the background and multithreads but then we cant have everything :D/>/>