Game Development Community

I <3 delegates!

by Manoel Neto · in Torque 3D Professional · 03/10/2010 (11:10 am) · 4 replies

I just needed to post about how much these guys have made my life easier. They're amazing for AI: I can have small logic blocks in separate delegate functions and "assemble" behaviors by combining them. I know they've been around since TGEA 1.7 or something, but only recently I started using them for my own purposes.

Oh, and signals are awesome =)

#1
03/10/2010 (11:28 am)
To quote a TV legend: "What you talking about Willis!".
#2
03/10/2010 (11:43 am)
"It's magic!"

Include the header:
#include "core/util/delegate.h"

Declare a member/variable:
// This delegate takes no arguments and returns nothing
Delegate<void()> myDelegate;

Bind some object and method to it:
myDelegate.bind(this, &MyClass::MyMethod);

Call it!
myDelegate();

It only takes two ASM instructions to call up the method, so it's likely to be faster than calling a virtual method. It's perfect for callbacks, because the caller doesn't need to know *nothing* about the callback, so it means less includes.
#3
03/10/2010 (11:43 am)
Manoel, Check out the MatrixSet class if you want to see some wild-and-crazy delegate action ;)
#4
03/10/2010 (11:46 am)
@Pat: oh, I've wandered there myself. Had to, in order to fix a bug with one of the inverse functions ;)

It's also amazing the uses a vector full of delegates can have.