Page 1 of 1

Why isnt this working?

PostPosted: Wed Jun 15, 2011 7:19 pm
by ThaKid244
Hi. I am in the middle of creating a new game but I am having a problem with variable counting.
I have a variable named "P".
I have an actor (Pcounter) that has animations of numbers eg. "one" "two" "three" "four" "zero"
When "P" equals one "one" is displayed, as for two and so on.
I have another actor which is supposed to (upon creation of an actor) make P = 0;
It is not working for some reason because my Pcounter always displays a one.
Can someone help?

Re: Why isnt this working?

PostPosted: Wed Jun 15, 2011 8:22 pm
by foleyjo
I think it's your if statement

I think it should be
Code: Select all
if (p==0) etc
if(p==1)etc


Also a switch statement might be better

Code: Select all
switch (P)
{
case 0:   ChangeAnimation("Event Actor", "zero", FORWARD); break;
case 1:   ChangeAnimation("Event Actor", "one", FORWARD); break;
}

Re: Why isnt this working?

PostPosted: Wed Jun 15, 2011 8:30 pm
by foleyjo
Actually the better people on this forum might suggest you also try

Code: Select all
ChangeAnimation("Event Actor", getAnimName(P) , FORWARD);

Re: Why isnt this working?

PostPosted: Wed Jun 15, 2011 10:29 pm
by ThaKid244
Thanks foley. Could you explain what the case statement does and why is there a "break;" afterwards?

Re: Why isnt this working?

PostPosted: Thu Jun 16, 2011 12:06 am
by skydereign
That is just how a switch statement works. The variable in the () at the beginning of the switch is the variable you are checking (like in an if statement). The actual cases are the values. So case 0 means if the variable equals 0.
Code: Select all
switch(var)
{
    case 0:
    // execute code here until the next break statement
    break;

    case 3:
    // execute this code here until the next break statement
    break;
}


So as the above code says the break is a way of determining the end of a case statement (kind of like {} in an if statement.

Re: Why isnt this working?

PostPosted: Thu Jun 16, 2011 7:24 am
by foleyjo
skydereign explained it better than I could.

The break statement exits the switch. If you don't have it the next case will also be used so if P = 0

Code: Select all
switch (P) 
case 0: X = 0;
break;
case 1: X = 1;
break;



Will make X = 0

Code: Select all
switch (P)
case 0: X = 0;
case 1: X = 1;


Will make X = 1

I know this because I have made the mistake of not using break before