ChucK - storing id values of sporked shreds

83 Views Asked by At

A little bit of a beginner so bear with me. I was writing a bit of code to experiment with shred sporking and removing, but encountered a problem. Here is a portion of my code:

while(hid.recv(msg)) //Hid hid is above
{
    if(msg.isButtonDown()) //HidMsg msg is above
    {
        spork ~ test() @=> Shred @ s; //test is just an empty function
    }

    if(msg.isButtonUp())
    {
        Machine.remove(s.id());
    }
}

With this, however, I get the error "undefined variable 's'...". I could tell that since defining 's' only happens after msg.isButtonDown() is true, so I tried a different method.

while(hid.recv(msg))
{
    Shred s;

    if(msg.isButtonDown()) //HidMsg msg is above
    {
        spork ~ test() @=> s; //test is just an empty function
    }

    if(msg.isButtonUp())
    {
        Machine.remove(s.id());
    }
}

However, this results in the error "cannot remove: no shred with id 0...". I don't understand why s.id() would be 0? Shouldn't the chucking in the first if statement define s.id() as the sporked id? I can't seem to get past this.

Thanks,

Kevin Kim

1

There are 1 best solutions below

0
On

Shred s is scoped to the body of the while loop. You're creating a new Shred variable on each iteration of the loop. They're different references. Put the variable declaration (Shred s in this case) outside of the while loop.