Game Development Community

in an onCollision can i...

by rennie moffat · in Torque Game Builder · 03/22/2010 (10:20 am) · 4 replies

in an onCollision can i..


A. get the position of the %dstObject and record it as a variable that can be passed onto another function? Since onCollision have no %this, I find it a bit tricky.
//currently I am using something like this.
function cardEnemy::onCollision(%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
	if(%dstObject.class $= "tileClass")
	{	
		if(%scrObject.class $= "eCET1Class")
		%tilePos = %dstObject.getPosition();
		%srcObject.setTracer1Pos();
	}
}

function cardEnemy::setTracerPos(%this, %tilePos)
{
	%this.tracer1.setPosition(%tilePos);
}

B. use...
if(%scrObject.class $= "eCET1Class")
///this %srcObject is an object in a behaviorField in the behavior, not the owner.

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
03/22/2010 (11:13 am)
You don't need %this in onCollision since you have both the object sending the collision %srcObject and the object that receives the collision %dstObject.

To pass a variable to another function all you need to do is:
%myObject.myFunction(%passVar);
then you'd have:
function myObject::myFunction(%this, %passVar)
{
   //you can now use %passVar here.
}

For your second question, yes you can compare an object's class with an if condition.
#2
03/22/2010 (2:16 pm)
Thanks
In the first part then can I say
///onCollision
{
%tilePos = %dstObject.getPosition
%srcObject.doThis();
}
///passed to function
doThis(%this, %tilePos)
{
%this.objectWhichIsNotTheOwner.setPosition(%tilePos);
}




And in the second part I can do something like this?

if(%srcObject.class $= "thisClass");

where thisClass is different from the behavior owner's class all together?
#3
03/22/2010 (3:35 pm)
You have a few errors. First, you're not passing a variable to the doThis function. You need to put the variable you want to pass within the parenthesis. So instead of %srcObject.doThis(); you want %srcObject.doThis(%tilePos);

Next, you are mixing up methods with functions. With %srcObject.doThis(); you are telling TGB to call the doThis method that belongs to the object sending the collision. Your doThis is just a plain function without a namespace attached to it. So you will get an error in the console that it cannot find the command doThis.

In Torquescript you can have multiple functions with the same name if they are methods belonging to different namespaces. So:

function something::onCollision(%lots of stuff)
{
   doThis();
}

// calls this command
function doThis()
{
   //stuff
}

and

function something::onCollision(%lots of stuff)
{
   %srcObject.doThis();
}

// calls this command
function ObjectSendingCollision::doThis(%this)
{
   //stuff
}

Because I did not put anything in the parenthesis I did not pass any variables to the doThis function or method. In the example here both versions of doThis can exist in the same game because one of them is a pure function and the other is a method belonging to ObjectSendingCollision.
#4
03/22/2010 (3:43 pm)
For the second part of your question, again what you did is fine syntax-wise. You just need to make sure that you do not mix up %srcObject with %dstObject. If the %dstObject is the one with the behavior attached and its collision callback set to true, then every time the object receives a collision it grabs the class of the object sending the collision and compares it to thisClass. If the object has "thisClass" then you run what you write within the brackets of the if condition.