Page 1 of 1

Actor * issues

PostPosted: Sat Mar 17, 2012 12:44 pm
by joncb
I have created a function in Global Script to randomly place a number of clone actors "Bone" in the gaming area. the Function is:
Code: Select all
void SetBones(int noBones)
{
int i;
int xb;
int yb;

Bone_Cnt = 0;

    for(i=1; i<noBones; i++)
    {
         sprintf(Bones.text, "Set Bones %i",i);
 
         xb = rand(sizex);
 
         if (xb > sizex - 40)
             xb = sizex - 40;
 
         if (xb < 40)
             xb = 40;
 
         yb = rand(sizey);
 
         if (yb > sizey - 40)
             yb = sizey - 40;
 
         if (yb < 40)
             yb = 40;
 
        Actor * Bone_Cl;
        Bone_Cl = getclone("Bone.0");

        Bone_Cl->x=xb;
        Bone_Cl->y=yb;
    }
}


When I try to add the script I get the following error:
"Error Line 33: Unexpeceted declaration yb"

I did some debugging by commenting out the code working with yb "(yb = rand(sizey);" onward, and the error then relates to xb.

When I edit out the code relating to Bone_Cl "Actor * Bone_Cl;" onward - there is no error.

Any ideas??

thanks in advanced

Joncb

Re: Actor * issues

PostPosted: Sat Mar 17, 2012 2:16 pm
by joncb
Found a solution, I put the Actor *Bone_Cl; up with my other declarations at the top of the function. For some reason this worked :?

Re: Actor * issues

PostPosted: Sat Mar 17, 2012 7:01 pm
by skydereign
That isn't just a random coincidence. You need to declare variables at the start of a code block. Essentially a code block starts after a {, and ends at the }. Variables declared in a code block do not exist outside of it.