Game Development Community

OnCollision w/ If statement

by Jason T · in Torque Game Engine · 02/02/2006 (2:27 pm) · 8 replies

I have 3 objects in the world. The first 2 (which are from the same simGroup as each other but in a different simGroup as this item) are working fine but I need help with this one.

This collision should only update if the player has already collected the other two items.
To make it simple I just did a check for the client.score if it = 2 then the other 2 items have been picked up, the score should increment and end the game. If the other two have not been picked up then nothing should happen.

Im a php programmer so Im sure my syntax is off for TorqueScript. The 'return' line is a bit confusing as are the brackets.

Thanks for any help you can offer.

function SuperBomb::onCollision( %this, %obj, %col )
{
if(%col.getClassName() $= "Player")
{
%client = %col.client;
if(%client.score %= 2)
{
%client.score++;
commandToClient(%client, 'SetScoreCounter', %client.score);
%obj.delete();
%bombCount = bomb.getCount();
return;
// otherwise display victory screen

commandToClient(%client, 'ShowVictory', %client.score);
}
}
}

btw. is this the right place to ask code questions? The boards are huge and I hate posting out of thread.

#1
02/02/2006 (3:06 pm)
I think you've got two issues:

1. "%= 2" should probably be "<= 2".

2. commandToClient() probably belongs after the closing brace following it.

so, something more like this:

function SuperBomb::onCollision( %this, %obj, %col )
{
   if(%col.getClassName() $= "Player")
   {
      %client = %col.client;
      if(%client.score <= 2)
      {
         %client.score++;
         commandToClient(%client, 'SetScoreCounter', %client.score);
         %obj.delete();
         %bombCount = bomb.getCount();
         return;
      }
         
      // otherwise display victory screen
      commandToClient(%client, 'ShowVictory', %client.score);
   }
}

personally, i'd indent the code, it really helps visualize the flow of logic,
and also just personally i'd remove one level of braces by changing the "if is player then {.....}" to "if if Not player then return;".
#2
02/02/2006 (3:28 pm)
My problem is that I only what this simGroup to become active ONLY after the other 2 items have been taken.

If the score is 1 then I want this obj to be ignored. If the score is 2 than this obj can become active.

Im trying to find docs on the 'return' command as Im sure I can do what I want if I had a btter understanding of how that worked.

As I mentioned im a php programmer so Im just getting the hang of this syntax. Thanks again for your help.
#3
02/02/2006 (3:50 pm)
I'm not sure about simGroups becoming active,
but you've got some pretty definite syntax errors in there,
as i mentioned previously.

return is a widely used programming instruction which means to immediately leave the current function and resume executing the function which called it.

this means that your commandToClient as you originally have (had?) it will *never* execute,
because it immediately follows a return.
#4
02/02/2006 (3:59 pm)
Yeah Im completely lost. There really needs to be better documantation on torqueScript.
The below code is the original code that I am moding. It works fine.

'becoming active' is not whatI mean. Sorry for the confusion. What I mean is I only want the above code to pass if and only if the current client.score is 2. If it is not 2 then I want the function to stop. If the score is 3 I want the commandToClient to pass.

function TorqueLogoItem::onCollision(%this, %obj, %col)
{
if(%col.getClassName() $= "Player")
{
	%client = %col.client;
	%client.score++;
	commandToClient(%client, 'SetScoreCounter', %client.score);
	%obj.delete();
	%logoCount = logos.getCount();
		if (%logoCount > 0)
		return;
	// otherwise display victory screen
     	 commandToClient(%client, 'ShowVictory', %client.score);
}
}
#5
02/02/2006 (4:21 pm)
TorqueScript is syntactically similar to C/C++ or JavaScript.

Did you by any chance try the code i wrote ?


The reason the return doesn't always execute in the TorqueLogoItem example is that it's part of the "if" statement right before it, even tho there's no braces.
#6
02/02/2006 (4:55 pm)
Yeah I checked out your code. But it ignores the <=2 and just deletes the obj and adds ++ to the score no matter what the current score it.

I think Im coming at this the wrong way anyway. I did a seach in the entire root of torque and not one file uses these types of statements. I think I may need to learn how to 'properly' code in torquescript.

Thanks for all your help man, I appreciate it.
#7
02/02/2006 (5:11 pm)
LOL, man sometimes it pays to walk away for a moment. Your code worked fine just needed to reverse the int and move the final command around a bit.

It all works fine. Thanks again!

function SuperBomb::onCollision( %this, %obj, %col )
{
   if(%col.getClassName() $= "Player")
   {
      %client = %col.client;
      if(%client.score >= 2)
	  	{
                   %client.score++;
                     commandToClient(%client, 'SetScoreCounter', %client.score);
                    %obj.delete();
		     %bombCount = bomb.getCount();
	                         if (%bombCount > 0)
	                         return;
                                 commandToClient(%client, 'ShowVictory', %client.score);
                  } //score check
   } //player check
} //functoin
#8
02/02/2006 (5:20 pm)
Awesome!
glad you got it going.