Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created June 1, 2013 19:12
Show Gist options
  • Save jamesu/5691411 to your computer and use it in GitHub Desktop.
Save jamesu/5691411 to your computer and use it in GitHub Desktop.
// Torque3D example of how to intercept a callback in code.
// Callbacks in Torque3D are declared by the DECLARE_CALLBACK macro, and are implemented by IMPLEMENT_CALLBACK.
// Usually they invoke a function in TorqueScript, but there is also an option to invoke a function via a function pointer which
// can be obtained from mAddress in the relevant EngineFunctionInfo.
//
// You must add the following in EngineFunctionInfo:
//
// void* getAddress() { return mAddress; }
//
// Our callback function. This will override the console callback
class GuiButtonBase;
void myOnClick(GuiButtonBase *button)
{
Con::printf("Override onClick\n");
}
// Example function which overrides the GuiButtonBaseCtrl::onClick callback
DefineEngineFunction( TestCallbackAssign, void, (),,"")
{
// Find a useful export
EngineExportScope * scope = EngineExportScope::getGlobalScope();
for (EngineExport *theExport = scope->getExports(); theExport; theExport = theExport->getNextExport())
{
if (theExport->getExportKind() == EngineExportKindType)
{
EngineTypeInfo *typeInfo = static_cast<EngineTypeInfo*>(theExport);
if (typeInfo->isClass())
{
// We have a class. Look for the callback we need in the class scope
EngineExportScope * classScope = static_cast<EngineExportScope*>(theExport);
for (EngineExport *theSubExport = classScope->getExports(); theSubExport; theSubExport = theSubExport->getNextExport())
{
if (dStrcmp(theSubExport->getFullyQualifiedExportName().utf8(), "GuiButtonBaseCtrl::onClick") == 0)
{
// Found the function we want. Now all we need to do is assign the address to the myOnClick callback
EngineFunctionInfo *function = static_cast<EngineFunctionInfo*>(theSubExport);
void **addr = (void**)function->getAddress();
*addr = &myOnClick;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment