Game Development Community

onCollision, Behavior tutorial woes

by MarkSulli · in Torque 2D Beginner · 07/10/2016 (2:25 pm) · 7 replies

I'm hoping someone can give me a bit of help sorting out an onCollision issue I'm having.

I'm running through the Behavior Tutorial(https://github.com/GarageGames/Torque2D/wiki/Behavior-Tutorial#chapter-4-adding-real-game-play----motivation) and it's going rather well...

Until I try to follow along with having my fish class collide with the fishfoodclass.

From what I can tell the collision is happening but inside of the lifeTimer script things go awry.

I've added:

function LifeTimerBehavior::onCollision(%this, %object, %collisionDetails)
{
if (%object.class $= "FishFoodClass") {
%this.modifyLife(%object.nutrition);
%object.recycle();
}
}

but this function seems to never get past the if (%object.class $= "FishFoodClass") check. So the food never gets .recycle() and the fish never gets .modifyLife(). I'm guessing there's an issue and I'm not really aware of how $= works. In the previous tutorial for the spaceship colliding with asteroids, the spaceship was it's own script and could refer to the asteroids as:

if(%sceneObject.getScenegroup() == 20)

I've tried this and it doesn't pass the check either. So what going on here that the code refuses to check back at either the SceneGroup or Class attributes? Am I just being dumb? Where might one having these sorts of issues go if they're having trouble in the documents that are supposed to explain these concepts?

I've checked for any typos and there aren't any that I've been able to locate. The setCollisionGroups for the fish and the SceneGroup for the food appear correct(And through testing I've been able to determine that the collision happens - it just refuses to execute the logic).

Hopefully someone can give me an idea on where to look next, or maybe at least explain how $= works as I've never seen that in other languages.

About the author

Recent Threads


#1
07/10/2016 (6:24 pm)
Okay, after banging my head against the problem for a while, I figured out that with the onCollision() function, it seems like for the three variables:

onCollision(%this, %object, %CollisionDetails)

The three actual parameters, %this, %object and %collisionDetails are not really sensical.

%this is.... I'm not sure, maybe that's my problem, but it has no class.
%object is -also- the object initiating the collision,
and % collisionDetails is the object %this(or%object) is colliding with.

This is definitely not how onCollision worked in the getting started tutorial. Is there a reason for this, have I somehow altered it?
#2
07/10/2016 (7:14 pm)
Hi Mark Sullivan ,
in your on collision function , check and see what the values are . just like , %ObjClass = %obj.class And echo the result to the log file or console window to see if it is fishfood or not . then you can proceed with the remedy . BTW the "%this" is/should be the objects datablock id and the obj should be the objects id I havent heard of colloision details before so i cant imagine what is is or where its coming from . so just try ColDetails = %collisiondetails if you need to see what it is . I like the idea of colliding with fish food . seems like your food could just be item class , like a health kit or something . just try %this.getclassname() name or %obj.getdatablock().getclassname() . subject to errors :) good luck
#3
07/10/2016 (8:40 pm)
James,

That works to sort out the problem, thanks for the info. You mentioned you don't know %collisiondetails, then that's likely thrown in my the author I would assume?

I figure, onCollision is a function provided by the engine, since while I did write the code it seems to catch collision data on it's own, but how do I know what order it wants to recieve that data, for instance? Where can I view the guts of that sort of thing to check when I'm having these sorts of issues?

For instance if I want to get info on windows hooks I can search MSDN, is there a functional equivalent that I haven't stumbled upon for Torque?
#4
07/11/2016 (6:41 pm)
this is T3D documentation
docs.garagegames.com/torque-3d/official/

this is TGE documentation , might be useful if not just interesting
docs.garagegames.com/tge/official/

as far as the engines work on collision detection , i havent a clue but you can see any activity that happens within your function by echoing messages and using the arguments that are provided . thatll also show you the order in which things are processed , I guess :). youll have a few options on controlling that as well , using variables , or toggling , whatever you can imagine really . Its quite surprisingly flexible at that point , within your ::oncollision function , your fish might decide to sleep it off after a nutriient packed treat :D.

there are some books available , you can find them on EBAY just search torque game engine or T3D , you can try e-books . and visit torque3d.org
torque3d.org
. But , if you havent be sure and check out the documentation , scripting , object methods , console functions , engine reference <-a little more advanced I think .

keep an eye on the blogs and resources here . use the search utility to search the forums , blogs and resources , check a few big name profiles and look through their activities .

#5
07/12/2016 (8:46 pm)
Thanks again James,the TGE documentation is very much what I was looking for.

I'm not sure if I'll be able to find the specific function in there(Haven't searched extensively yet) but next time I'm going to use it I'll be looking it up. That's a pretty great resource.

For anyone else wondering about this sort of stuff, I've also found this:
http://garagegames.github.io/Torque2D/TorqueScriptDocs/html/index.html

which looks like a good resource for function information. I get the feeling I've swam into the deepend at this point but nothing ventured...

Thanks for all the help, James!
#6
07/12/2016 (10:20 pm)
Mark , In case you havent seen these ,
but first , I only use a demo version of TGE , but open your console with the tilde key , and type dum then hit the tab key , with the TGE demo this will begin to show the functions such as . dump and dumpconsoleclasses , you see . you can use any letters its like a shortcut to functions , just keep tapping the tab key and shift tab will go backwards through them . but I thought you might be interested in dumpconsoleclasses() and dumpconsolefunctions() . theres another used with object ids called .dump() used like %obj.dump() or object id -> 1234.dump() . itll show you the methods available for that object :) hehe swam into the deepend , funny .
#7
07/12/2016 (10:31 pm)
Try this:

function LifeTimerBehavior::onCollision(%this, %object, %collisionDetails)
{
    %classname = %object.getClassName();
    if (%classname $= "FishFoodClass") {
        %this.modifyLife(%object.nutrition);
        %object.recycle();
    }
}

The examples in that may be somewhat outdated. I don't believe that field is exposed that way anymore.