Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

Callback function within a function?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 26 July 2014 - 10:26 AM

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


Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
1Likes
Like

Posted 26 July 2014 - 03:28 PM

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

#3 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 26 July 2014 - 04:10 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS