🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Callback function within a function?

Started by
1 comment, last by Paul C Skertich 9 years, 11 months ago

Say for instance I wanted to add a callback within a function. In HPL Engine there's quite of them called: AddTimer(const string& asName, float time, const string& asFunction); Usage would be given:


void onEnter() {
    AddTimer("Timer1",2.0, "Funct1");
}
void Funct1(string &asFunction) {
//... Do something ..//
}

I don't know why but I got a feeling inside AddTimer C++ function they create a new module and build it upon initialization. The declared function being whatever the third parameter be inside AddTimer. Upon rendering time - they would execute all the modules functions?

Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX
Advertisement

Most likely they are doing something like this:

// C++ implementation
void AddTimer(const string &asName, float time, const string &asFunction)
{
   // Find the function that should be called
   asIScriptModule *mod = engine->GetModule("game", asGM_ONLY_IF_EXISTS);
   asIScriptFunction *func = mod->GetFunctionByName(asFunction.c_str());
 
   // Store the function and argument for when it is time to call it
   ...
}
 
void CallTimerCallback()
{
   // Retrieve the function to call and argument from the storage
   ...
 
   // Prepare a context and call the function
   asIScriptContext *ctx = engine->RequestContext();
   ctx->Prepare(func);
   ctx->SetArgObject(0, asName);
   ctx->Execute();
   
   engine->ReturnContext(ctx);
}

You can get more information on how to use callbacks from the manual.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Most likely they are doing something like this:


// C++ implementation
void AddTimer(const string &asName, float time, const string &asFunction)
{
   // Find the function that should be called
   asIScriptModule *mod = engine->GetModule("game", asGM_ONLY_IF_EXISTS);
   asIScriptFunction *func = mod->GetFunctionByName(asFunction.c_str());
 
   // Store the function and argument for when it is time to call it
   ...
}
 
void CallTimerCallback()
{
   // Retrieve the function to call and argument from the storage
   ...
 
   // Prepare a context and call the function
   asIScriptContext *ctx = engine->RequestContext();
   ctx->Prepare(func);
   ctx->SetArgObject(0, asName);
   ctx->Execute();
   
   engine->ReturnContext(ctx);
}

You can get more information on how to use callbacks from the manual.

Thanks Andreas - I just figured it has to be like that almost. I viewed the HPL 1 engine source code and it was entangled into a lot of things but the summary was that it was just like you mentioned. There was a callback linking back to the scripting interface. It was pretty confusing to follow though how they achieved it.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX

This topic is closed to new replies.

Advertisement