Game Development Community

Is this if statement right?

by Isaac Barbosa · in Torque Game Builder · 12/28/2006 (2:39 pm) · 7 replies

Hello:

I'm using the whack-a-mole tutorial code to make some experiments -I'm learning-. So I have add a variable set to false to check if the mole was already hitted or not.

Original code is:

function Mole::onAdd(%this)
{
      %this.setUseMouseEvents( true );
      %this.diveInEventId = %this.schedule(getRandom(1000)+2000,"diveIn");
}

Actual code is:

function Mole::onAdd(%this)
{
      %this.whacked = false;
      %this.setUseMouseEvents( true );
      if(%this.whacked == false)
      {
        %this.diveInEventId = %this.schedule(getRandom(1000)+2000,"diveIn");
      }
}

the point here is that in this case the true and false conditions works as expected... but if I leave the original onAdd function as it is and I put my code in the onAnimationEnd, it doesn't works. I want to leave the mole static in the last moleComeOut frame if it was hitted, and leave it go to the dive in animation if not hitted:

function Mole::onAnimationEnd(%this)
{
     if( %this.getAnimationName() !$= ("badBallComeOut" @ %this.moleColor) )
        {		    
         // update score
            if( %this.getAnimationName() $= ("badBallWhacked" @ %this.moleColor) )
               {
                  if(%this.whacked == false)
                     {
                       %this.diveInEventId = %this.schedule(getRandom(1000)+2000,"diveIn");
                        }
                      }    
                    %this.scenegraph.incWhackedCount();
                     else
                    %this.sceneGraph.incMissedCount();
		
      //%this.safeDelete();
	  }
}

but the truth is that I'm lost. I see that the false/true variable works if inside on the onAdd function but not in other functions??? I hope this make some sense.

Thanks in advance

#1
12/29/2006 (4:33 pm)
Wait, I'm unclear about something - if you revert back to the original onAdd function, are you also removing this line of code?
%this.whacked = false;
The reason is because if you're not defining this value, then when you use it in onAnimationEnd it will be created with a value of "", which isn't false, hence your decision statement will not evaluate properly.
#2
12/29/2006 (4:59 pm)
@Drew:

I can't understand that at all, and I'm lost now with another issue. I will try this later. Thanks!
#3
12/30/2006 (8:23 am)
In Torquescript you can dynamically create data fields without having to initialize them. This is cool, but can cause some trouble when you try and do something like this
if (myObj.newField == false)
    // do code
In the above example, I am using "newField" for the very first time. When you do this, Torquescript assigns a value of "" (literally - if you were to echo the value of "newField" to the console it would print a blank). Since "" is not a numeric value and false is 0, the if statement won't work. You have to do this
myObj.newField = false;
if (myObj.newField == false)
    // do code
So all I'm trying to make sure is that you are keeping this line of code
%this.whacked = false;
in your onAdd function, otherwise the decision statement in your onAnimationEnd function won't work
#4
12/30/2006 (8:55 am)
Thanks for the explanation!

I will keep this on mind when dealing with if statements ;)
#5
01/01/2007 (12:04 pm)
Still lost with the false/true statements in TGB (not in flash :))...

I have this onAdd function:

function Square::onAdd(%this)
{
     %this.setUseMouseEvents( true );
     %this.respawnPoint.isNotGold = true; //so this variable is supposed to be set to true in the onAdd event.
     %this.turnOffEventId = %this.schedule(getRandom(1000)+2000,"turnOff");
}

function Square::onMouseDown()
{
//when I click in my square I call countIn function, this works as expected...
     %this.countIn();
}

function Square::countIn(%this)
{
   if(%this.respawnPoint.isNotGold $= true) //since the isNotGold variable is set to true, this works.
   {
   %this.respawnPoint.setImageMap("gridgoldImageMap");
   %this.respawnPoint.isNotGold = false; //and it is supposed that the variable is now set to false!!!
   %this.scenegraph.incGoldCountMore();
   }
   %this.scenegraph.incWhackedCount();
   %this.respawnPoint.isOccupied = "";
   %this.sceneGraph.spawnPointsOccupied -= 1;	
   %this.safeDelete();
}

function Square::getRid(%this)
{
     if( %this.getAnimationName() $= ("badBallComeOut" @ %this.ballColor) )
     if(%this.respawnPoint.isNotGold $= false) //this is not working... everything is working as if true were always true!!!
    {
      %this.respawnPoint.setImageMap("gridsilverImageMap");
      %this.respawnPoint.isNotGold = true;
      %this.scenegraph.incGoldCountLess();
     }
     %this.playAnimation("badBallDiveIn" @ %this.ballColor);
}

I hope this make some sense... can't see what's the problem :(

Regards
#6
01/01/2007 (2:20 pm)
Isaac, there are a few flaws with your comparisons.

%var $= true
%var $= false

Will likely both always return true or always return false because $= is a string comparator and you're comparing them to ints (true is 1 and false is 0). If %var is a string that equates to "true" or "false" then you've got to use $= "true" and $= "false" but if %var is a boolean that equates to true or false then you use the %var directly as in

if (%var)
{
do something
}
#7
01/01/2007 (3:38 pm)
@Ben:

Thanks for the clarification Ben. I don't know if I'm doing well, but I have made some changes and now the program is working as i want to do. I think that it has to do with the functions scope and the variable itself. I'm very happy with the TGB power. :)