Game Development Community

Trigger Region

by Rich Wilson · in Torque X 2D · 01/24/2007 (11:33 am) · 30 replies

What's the best way to make a trigger region that I can detect player collision with? I'm trying to make doors between rooms and I want to load a new level as soon as the player enters a rectangular area.
Page «Previous 1 2
#1
01/24/2007 (12:54 pm)
Put a rectangle on the screen where you want the 'trigger area' to be. Give that rectangle a collision and physics component. In the collision component, set the 'onCollision' to the method which loads the new level.

#2
01/24/2007 (3:08 pm)
Actually I would prefer using a T2DTriggerComponent instead of collision and physics components. The TX trigger component works much like TGE and TGB triggers where you have OnEnter, OnStay, and OnLeave callbacks. You can attach the trigger component to your door sprite or place a new scene object in TGBX and hang it off that. Scene objects are not visible so they are a good choice for "hidden" triggers.

#3
01/25/2007 (11:52 am)
If I were going the T2DCollisionComponent route, where do I make the method that OnCollision will point to? I see the drop down box in the component properties, but how do I make something that will show up in it?

I don't even see a T2DTriggerComponent in Torque X. Is that something from TGB that hasn't been ported over yet?
#4
01/25/2007 (12:39 pm)
If you want to use the collision component I think the easiest way right now to specify the OnCollision is in code. Alternatively you can see if any of the included examples specify lists of delegates in their componentSpec.ed.cs files for an example of how to get it in the editor. That's the easy part. The hard part is finding which script component to hang the field off of.

#5
01/25/2007 (2:44 pm)
@Rich - Ben is correct, you really should use the Trigger Component. I don't know why it's not being pulled, but apparently several torquecomponents aren't in TGBX yet. Just open up componentSpecs.ed.cs in your solution and add the following and you'll be able to use the Trigger Component through TGBX:

$T2DTriggerComponent = TorqueX::RegisterComponent( "T2DTriggerComponent", "GarageGames.Torque.T2D" );
$T2DTriggerComponent.addField( "Enabled", "Bool", "false" );
$T2DTriggerComponent.addField( "LayerMask", "Int", "false" );
$T2DTriggerComponent.addField( "CollidesWith", "ObjectTypeList", "" );
$T2DTriggerComponent.addField( "IgnoreCollCompNullWhenImgs", "Bool", "false" );
$T2DTriggerComponent.addField( "OnEnter", "nameRef", "", "T2DTriggerComponentOnEnterDelegate" );
$T2DTriggerComponent.addField( "OnStay", "nameRef", "", "T2DTriggerComponentOnStayDelegate" );
$T2DTriggerComponent.addField( "OnLeave", "nameRef", "", "T2DTriggerComponentOnLeaveDelegate" );




www.linkedin.com/img/webpromo/btn_viewmy_160x25.gif
#6
01/25/2007 (3:11 pm)
So in this case I'd inherit from T2DCollisionComponent, something like LevelTransCollisionComponent. Give it a string property for the level I'm pointing to, and maybe one for the destination node name, and then handle the jump in the OnCollision method for that component.
After that, would I make a scene object with that component and template it, so I can have level transition rectangles to just drop into the editor?
#7
01/25/2007 (4:20 pm)
Rich, until you have a build of TGBX that includes the trigger component you will need to use code similar to what Jonathan posted to add it to your build or just enter it in the scene file manually. If you choose the manual route you have to take care when opening the level in TGBX again or it will delete the object when you save the level. I'm sure this will be worked out by GG before release but that is the trade off for using beta software. So if you use the code snippet as a starting point you can create a drag-n-drop editor solution, otherwise you'll be doing it manually.

#8
01/26/2007 (7:16 am)
So I need to follow those steps to register the component to be able to inherit from it in code? Either way I'm going to have to hand code my own collision (or onTrigger) method, right?
#9
01/26/2007 (7:18 am)
@Rich - Pasting the code in above will open the trigger component up to TGBX. You can inherit from it in code right now by name as long as you have a using statement for T2D




www.linkedin.com/img/webpromo/btn_viewmy_160x25.gif
#10
01/26/2007 (12:21 pm)
...and once you do inherit from it in code you'll want to modify the code above to additionally expose your custom members to TGBX.

#11
01/30/2007 (7:20 am)
Ok. Sorry this is such slow going. I'm only able to pick away at this stuff during my lunch breaks. Here's where I'm at.
I have the inclusion block above in my componentSpecs.ed.cs to make the project recognize the T2DTriggerComponent

I can attach this component to objects in my scene if I want as well.
Where I'm getting hung up is how to customize the thing.

If I want to use the component as is, I don't know how to point OnEnter to a custom method.

If I want to make an object that inherits from the trigger component, I don't know where to find the code or spec for T2DTriggerComponent so I know how to structure my child component.

Is it as easy as making a component child of T2DTriggerComponent and only including an OnEnter method? I figured there's some other boilerplate stuff I would have to include in my own version of the component.
#12
01/30/2007 (8:15 am)
Quote:If I want to use the component as is, I don't know how to point OnEnter to a custom method.

Here's some hacky, syntax imperfect code to set the callback
TriggerComponent trigger = myObject.FindComponent<TriggeryThing>();
trigger.OnEnter = OnEnter; // where OnEnter is a method you have defined somewhere

Quote:Is it as easy as making a component child of T2DTriggerComponent and only including an OnEnter method?

Almost!
public class MyTriggerComponent : T2DTriggerComponent
{
    public void EnterCallback(some args in here to match the OnEnter delegate) { ... }
    protected override bool _OnRegister(some args here)
    {
        if (! base._OnRegister)
            return false;
        OnEnter = EnterCallback;
        return true;
    }
}

Quote:I figured there's some other boilerplate stuff I would have to include in my own version of the component.

If you were making your own version of the component, then yes. However you are not making your own version of T2DTriggerComponent, you are extending it. So everything it NEEDS to do is already done in the base class.

Hope this helps a bit!

#13
01/30/2007 (11:08 am)
I found the OnEnter delegate by right clicking the T2DTriggerComponent keyword in the IDE and choosing 'definition'. It showed me the methods, etc, but there are no parameters for the OnEnter delegate. Not sure what that means. Delegates must be a C# thing I haven't come across before.

Also, how is EnterCallBack tied to OnEnter? Where is that relationship defined? I'm not sure how I would have guessed that EnterCallBack would be the method I needed to enter instead of my own OnEnter method.

Thanks for your patience guys. I feel like I'm slowly but surely getting a grasp of this system.

-edit Nevermind, I just saw the OnEnter = EnterCallback line in the code sample. Moving forward...
#14
01/30/2007 (11:47 am)
Don't worry about it if it's slow going Rich, at least you are improving which is all that matters. The forums are here for people to ask and hopefully receive help no matter what skill level. Your questions aren't stupid and the answers aren't always obvious *cough* typename *cough* (sorry, inside joke)




www.linkedin.com/img/webpromo/btn_viewmy_160x25.gif
#15
01/30/2007 (12:01 pm)
I should probably go back and bone up on C# before proceeding with this to any serious degree. I've got a C/C++ background, and there are a lot of new twists I have yet to understand.

I now have a scene object placed with the trigger component attached, but I'm not getting any response. I put a breakpoint in my EnterCallback method and it's not firing. I've tried this with collision and physics components attached to the scene object to no appreciable result.

One thing I changed was the _OnRegister, since it was giving me a compile error. I used the InitComponent method instead. (what's the difference between the 2? ). Not certain if that would be an issue.
#16
01/30/2007 (5:41 pm)
Rich, "delegate" is the C# word for "function pointer" in c/c++

To find the args for OnEnter you'll see that OnEnter is of type T2DTriggerComponentOnEnterDelegate
If you go to the definition for T2DTriggerComponentOnEnterDelegate you'll see the declaration of the delegate (function pointer) and its signature.

#17
01/30/2007 (8:44 pm)
As I mentioned above, I'm not getting any response from my new component object's OnEnter method. I put a breakpoint inside the method and it's not firing at all.

Interestingly enough, I also put a breakpoint inside the InitComponent method and it didn't fire either. This component is attached to a SceneObject I dragged off of the toolbar. Is there some other way I should be placing this component in the scene?

I tried tacking it onto a sprite object, but when I did that the SceneLoader kept throwing an exception. Any ideas?
#18
01/30/2007 (8:49 pm)
1. Which version of the beta are you using?

2. Did you remember to set the CollidesWith for your trigger?

3. Does your OnEnter method have the proper args?
#19
01/31/2007 (11:38 am)
1. v1.1.3 on both TGB and T2D according to the editor

2. I have collidesWith t2dSpriteType in the triggercomponent's properties.

3. I checked the OnEnter delegate definition and found:
(T2DSceneObject ourObject, T2DSceneObject theirObject)

The fact that InitComponent isn't firing concerns me. That's the method that sets OnEnter to EnterCallback. What's the difference between _InitComponent and _OnRegister? _OnRegister wouldn't even compile, so I used the former instead.
#20
01/31/2007 (1:59 pm)
1. I mean which version of the TorqueX beta ;)

2. Maybe try assigning a type of "PlayerType" to your player and add "PlayerType" to the CollidesWith of the trigger

3. If your code that assigns EnterCallback to OnEnter is never being called then that is probably the root of all your problems. We need the answer to #1 to really hone in on that problem. There are two main reasons your init method is not called:
- it is the wrong method, or
- the component is not a component of an object that was added to the scene

What compiler error did you get when trying to use OnRegister?

Page «Previous 1 2