On 8/3/07, Martin Ahnelöv <operagasten@gmail.com> wrote:
> loops, but wanted them to run "side-by-side", but looping at different
> speeds, how would you do it?
>

 

Yes, you can do that. FYI, it's called threading in traditional
programming languages and is often a pain in the ass, but since chuck
isn't a traditional language it's called sporking and is as simple as
running a chuck file.

That's right!

 

You can also do sporking in the code, but I haven't looked at that
enough - it's in the help pages, though.

That's also quite simple. You can run threads (we call them shreds) next to the main one. You define those as functions and you "spork" the function.
=======================================
//let's define a function
fun void paralel()
{
while(true)
    {
    //this will loop forever
    <<<"beep">>>;
    second => now;
    }
}
 

//let's try sporking it
spork ~ paralel();

//now we write the main loop that will run next to it
while(true)
{
//this will also loop forever
<<<"boop">>>;
.75::second => now;
}


//anything that you would write here will never get executed because the program will always be stuck in that last loop. you could still define functions here though.

-----------------------------

See? not at all hard. You can run as many functions next to eachother as you like, they can all be different or they could all be the same. If they are all the same you only need to define it once and can spork it as many times as you'd like (untill the cpu gives up! Notice that they can all run at different speeds if you'd like, in my example above one loops in .75 seconds while the other takes a whole second so they will drift in and out of phase.

Cheers,
Kas.