Game Development Community

Simple Question about If else statements.

by Ecliptic · in Torque Game Builder · 01/10/2009 (8:13 pm) · 5 replies

I am having a hard time with Torques If() else () statements. Everytime I use these statements it will always go with the first part of the statement no matter what. Here is one for example, It should look to see if the object is visible, if it is then hide it, if it is not then continue on with the function.
function Image::onMouseDown(%this, %modifier, %worldPos)
{
	if($cheat.visible =1)
	{$cheat.visible =0;}
	else{
   // Toggle the drag status.
   %this.dragging = !%this.dragging;
   
   // Get position of starting point.   
   %newXPos = getword(%worldPos,0);
   %newYPos = getword(%worldPos,1);	
   
$oldX = %newXPos;
$oldY = %newYPos;

//Removing the negative from the position so I know which direction I need to be moving in.
   if ($oldY < 0) 
   { %posmod=-1;} else { %posmod = 1; }
   $OLDNEUTRALY = $oldY * %posmod;
		
   if ($oldX < 0) 
   { %posmod=-1;} else { %posmod = 1; }
   $OLDNEUTRALX = $oldX * %posmod;
   
//On the mouse down it will play this animation so it looks selected.
	if(%this.getAnimationName() $= ("Overlay"))
		%this.playAnimation("OnSelectAnimation");
	echo("Image has been selected");
}
	}

Can someone please explain this to me?? I guess the is something I am doing wrong?

#1
01/10/2009 (9:46 pm)
= is assignment. == is comparison. So your first if "($cheat.visible = 1)" doesn't check to see if the object is visible, it makes it visible.
#2
01/11/2009 (9:31 am)
In addition to what Sherman said about the difference between = and ==. there's also $= for string comparisons.

%val = 10; // note ONE = sign for assignment

  if (%val == 10 ) // note TWO == signs for number comparison
     echo("It's equal");

  %val = "Hello";       // ONE = sign for assignment

  if ( %val $= "Hello" ) // Note $= rather than == for string comparison
     echo("It's equal");
#3
01/11/2009 (9:42 am)
Ack, wow I feel very stupid. Thanks guys, that explains a lot and it is mentioned in the reference guide.

Thanks again
Dane
#4
01/12/2009 (9:02 pm)
Dont worry most of us been there and done that.
#5
01/16/2009 (11:06 am)
Like Ehrlichmann said, we all have done that so don't feel bad :)