problem with IF
Posted:
Thu Nov 16, 2006 2:09 am
by will97
i'm not an expert (some of you will thought that i am stupide but...)
i need help with the IF thing
can somebody here can tell me how to use it
(i tryed a lot of thing and they all dont work)
- Code: Select all
if ()
{
}
Posted:
Thu Nov 16, 2006 5:05 am
by Fuzzy
an example..
if (something is tested)
{
do something here if the test is true
}
is how it works. now as for what to fill in..
if (Jumping==1)
{
Here is code to make him fall
}
see the double ==? that means compare. if you only use one = then it means assign.
so...
x = 100;
means make x contain the value 100
x == 100
means compare the value of x to the value 100
if you use a single = in an if sign, it assigns that value, and the if statement acts true, causing the instuctions within the {} brackets to be executed. Its a common error.
What if you wanted to see if something is not true? use this
if (jumping != 1)
{
allow character to jump code
}
! means not, so != means not-equal
if (x > 100)
means check to see if x is greater than 100.
if (x < 100) means check to see if it is less than 100.
if (x <= 100) is greater than or equal.. and the opposite is >=
you may also combine two tests in one if...
if ((x > 100) && (Jumping == 1))
&& means 'and'
|| means 'or', so if either test passes, do the code.
you may also occasionally use
if ((test == 1) ! (jumping == 1))
which means if the first, but NOT the second is true, execute the code.
You may also see
if (test)
{
}
which presumes that the variable is true, or positive.
and...
if (!test)
which means its not true..
hope this helps. if you have a defective If statement, post it here, and we will examine it for errors.
Posted:
Thu Nov 16, 2006 10:17 am
by sonicfire
and dont forget the else statement:
- Code: Select all
this = 0; (this is false)
if (this)
{
// do something
} else { // since "this" is 0
// do something else
}
you can also add more if-else-statements if you like:
- Code: Select all
this = 0; (this is false)
that = 1; (that is true)
if (this)
{
// do something
} else { // since "this" is 0
if (that == 1) // or just write "if (that)" (if that is true)
{
// do that
}
}
Posted:
Thu Nov 16, 2006 10:39 pm
by Game A Gogo
and also the
else if command, it is use when you want to check something like this:
- Code: Select all
if(var==1)
{
//do something
}
if(var==2)
{
//do something else
}
if(var==3)
{
//do something
}
if(var>=4)
{
//do something
}
You could turn the code like this:
- Code: Select all
if(var==1)
{
//do something
}
else if(var==2)
{
//do something else
}
else if(var==3)
{
//do something
}
else if(var>=4)
{
//do something
}
that could augment a little the frame rate when the action is executated, this is useful in the draw actor.
but remember, you still can do this:
- Code: Select all
if(var==1)
{
//do something
}
else if(var==2)
{
//do something else
}
if(var2!=1 && var==3)
{
//do something
}
else if(var2>var)
{
//do something else
}
And if you just have ONE line of code, you still can do this
- Code: Select all
if(var<=10)//put the action here
instead of
- Code: Select all
if(var<=10)
{
//action here
}
hope this helps also!
Posted:
Sun Dec 03, 2006 3:16 pm
by Game A Gogo
it because there isn't a line break at the end of the IF statement i think.