Game Development Community

a bool return, but no if Question; basic programming skills.

by rennie moffat · in Torque Game Builder · 01/31/2010 (2:11 pm) · 8 replies

this is probably more a basic coding question rather than specific to Torque, but why does this line, from shooter tutuorial.

in it it asks for a getIsAnimationFinished()

now it returns a bool, so my question is, why in the next line does it not need an "if true" statement? Should it not read, if %this true, then %this.playAnimation(PlayerRunAnimation)

if ($player.moveLeft == true || $player.moveRight == true)
   {
      if (%this.getAnimationName() $= "playerRunAnimation")
      {
         if(%this.getIsAnimationFinished())
         {
            %this.playAnimation(PlayerRunAnimation);
            return;
         }
      }else
      {
         %this.playAnimation(PlayerRunAnimation);
      }
   }else
   {
      %this.playAnimation(PlayerStandAnimation);
   }

About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
01/31/2010 (2:44 pm)
Are you getting confused by the multiple branching conditions in your example?

If the player is moving left or right AND if the current animation is the run animation AND if getIsAnimationFinished is true (bool of 1) you play the run animation and return out of the entire function. If the animation is not finished (bool of 0) then the condition is false and you exit out of all branches without doing anything as there is no else condition for the getIsAnimationFinished branch.

If the player is moving left or right and the current animation is not the run animation, you play the run animation.

If the player is not moving left or right, you play the stand animation.
#2
01/31/2010 (3:42 pm)
Quote:
why in the next line does it not need an "if true" statement? Should it not read, if %this true, then %this.playAnimation(PlayerRunAnimation)

Because doing

Example code
if(%this.running)
{
   //Put your code here.
}

is the same as

Example code
if(%this.running == true)
{
   //Put your code here.
}

and doing

Example code
if(!%this.running)
{
   //Put your code here.
}

is the same as

Example code
if(%this.running == false)
{
   //Put your code here.
}
#3
01/31/2010 (4:13 pm)
ok but they have this...

if(%this.getIsAnimationFinished())



I thought it was in the open, to be determined as to whether it is true or false. My thinking was telling me that the programmer should have told, or said

if(%this.getIsAnimationFinished() == true)

#4
02/01/2010 (1:49 am)
The if statement takes only one parameter, a boolean value. How you supply that value is irrelevant to the compiler. The operators ==, !=, <=, >= etc are supplied so that you can test values. These operators act like functions and return that boolean value for you.

So in your last post there, the == true is superfluous when then method returns a bool already.

I should also point out, that your last line there, if getIsAnimationFinished() returns true, your statement:

if(%this.getIsAnimationFinished() == true)

equates to:

if (true == true) // do something

Superfluous.
#5
02/01/2010 (10:42 am)
cool,
so with in an if statment one can have a value returned. As in thisThingy().


The only thing is, I seemed to be developing a habit that a variable needed to placed inside if statments not an actual call.


For Instance...

%thisThingy = %this.getFrame();

if(%thisThingy == 3)
Do this



as opposed to
if(%this.getFrame() == 3)

however... this case may be different since it does not call a boolean.




#6
02/01/2010 (10:48 am)
but in this case

if(%this.getISAnimationFinished())
{
///will what is in here NOT be called/ran if the above boolean is false?
#7
02/01/2010 (6:57 pm)
The methods presumably returns true if the animation is finished, so yes, if it returns false, that block wont run.

%this.getFrame() == 3 returns a boolean. It either does equal 3 or it doesn't. True if it does, false if it doesn't.

The style choices you wrote about are just that, styles. Both equate to the same thing, and in a good compiler, they'll most likely compile to the same code too.

So,

%thingy = getThingy();
if (%thingy) // do something if thingy was true.
and
if (getThingy()) // do something if getThingy() was true.
Are the same thing to the compiler. The first example allows you to use the value again without calling the method though, which is useful.

WARNING: Confusing info ahead. If this confuses you, just ignore it.

Also note, just to confuse you, that 0 is false, and any other number is true, though true is typically represented as -1, because in a signed environment, the byte 0xFF is -1 rather than 255.

So, in C++ (I'm pretty sure TorqueScript too, but I don't remember for sure) you can say

if (3) // do something when the if statement returns true
And that code will be executed, because the value is not 0. It's the same as writing if (true).
#8
02/01/2010 (7:43 pm)
cool man thank you.