Actor * issues

Game Editor comments and discussion.

Actor * issues

Postby joncb » Sat Mar 17, 2012 12:44 pm

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
joncb
 
Posts: 10
Joined: Sat Mar 03, 2012 1:54 am
Score: 0 Give a positive score

Re: Actor * issues

Postby joncb » Sat Mar 17, 2012 2:16 pm

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 :?
joncb
 
Posts: 10
Joined: Sat Mar 03, 2012 1:54 am
Score: 0 Give a positive score

Re: Actor * issues

Postby skydereign » Sat Mar 17, 2012 7:01 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest