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

Program to test functions and parallel API. Why does only one function run continously?

Started by ctyking, 30 September 2012 - 01:49 PM
ctyking #1
Posted 30 September 2012 - 03:49 PM
I wrote a simple program to test functions and the parallel API.

The goal of the program is to have a blinking "A" and a blinking "B" near eachother on the screen, their blinkings controlled by separate functions, but ran simultaneously with the help of the Parallel API.

My problem is that only the "B" blinks, the "A" sits there doing nothing.

Here is the code I wrote:

http://pastebin.com/WQgHrUGj


Do you know what the problem might be?
Anonomit #2
Posted 30 September 2012 - 04:25 PM
The problem is that when you use term.setCursorPos() it isn't clearing the line that the cursor was on before the sleep() call. This is untested, but try this small revision:


    term.clear()
     
     
    function anim1()
    while true do
    term.setCursorPos(2,2)
    term.write("A")
    sleep(1)
    term.setCursorPos(2,2)
    term.clearLine()
    sleep(1)
    end
    end
     
    function anim2()
    while true do
    term.setCursorPos(5,5)
    term.write("B")
    sleep(1)
    term.setCursorPos(5,5)
    term.clearLine()
    sleep(1)
    end
    end
     
    parallel.waitForAny(anim1,anim2)

ctyking #3
Posted 30 September 2012 - 04:37 PM
Thanks! That solved it!
Anonomit #4
Posted 30 September 2012 - 04:45 PM
No problem. You really need to watch where the cursor is if you're using any print, write or read calls along with the parallel API. Use term.getCursorPos() if you need to quickly print something then return the cursor to where it was before.