TX/Tutorials and Guides/TX UserGuide/Exposing Delegates in TXB

From TDN

This page is a Work In Progress.

Contents


Return To Contents Page

Introduction

Torque X is a "general purpose" game engine. That means that the user needs to be able to configure lots of things to tailor it to their particular game. And delegates are a way to do just that. For example, instead of arbitrarily deciding that any collision between objects should make the objects explode, which would really limit the kinds of games you could make, Torque X has a delegate in the collision component which lets you define your own method (if you want) to determine what happens on a collision (called the "onCollision" callback). Let's look at how we would use that.

Custom OnCollision Example

First, we would need to define our method. The onCollision property of the collision component is defined as a T2DOnCollisionDelegate type. That type is defined like this:

public delegate void T2DOnCollisionDelegate(T2DSceneObject ourObject, T2DSceneObject theirObject, 
                                            T2DCollisionInfo info, ref T2DResolveCollisionDelegate resolve, 
                                            ref T2DCollisionMaterial physicsMaterial); 

So, what we need is a method that doesn't have a return value (i.e. a void method) that has arguments of the types in the definition. So we might define our method like this:

public static void SpinCausingCollision(T2DSceneObject us, T2DSceneObject them, T2DCollisionInfo info, 
                                        ref T2DResolveCollisionDelegate resolve, ref T2DCollisionMaterial physicsMaterial)
{
    us.Physics.AngularVelocity += 10.0f;
    them.Physics.AngularVelocity -= 10.0f;
} 

Custom Properties

Now our method is something that we could put into the onCollision variable of the collision component. Let's assume we want to do this assignment in TXB instead of code. How do we do that? Well, we need to tell TXB that this method is a valid choice for that variable. And the way we do that is to expose a property with that delegate type. Now, generally, we like to hide our internal variables and expose them in properties. So we would do something like this:

private static T2DOnCollisionDelegate   _myCollisionMethod = SpinCausingCollision; 

And we'd create a nice property to wrap that private variable, and expose it to TXB:

[TorqueXmlSchemaType]
static public T2DOnCollisionDelegate MyCollisionMethod
{
    get { return _myCollisionMethod; }
    set { _myCollisionMethod = value; }
} 

Updating The Schema

Because we've added the [TorqueXmlSchemaType] tag, building our solution will update the file "myscheme.txschema". (This update is done via a post-build event, that is automatically handled for you if you've created your solution from a Torque X project template such as StarterGame, etc.)

Now that TXB knows that there's a method out there that can fit into a T2DOnCollisionDelegate type, it will offer that method as a choice in the listbox when you try to set the onCollision value in the T2DCollisionComponent.

In this example we've been talking about exposing a custom OnCollision delegate, but there are plenty of other things in Torque X that use delegates to let the engine call user-defined methods. The principles, however, are the same.


Return To Contents Page

This is Community Maintained, you may, and probably will, find out of date material. Feel free to update any of these articles yourself.