Jump to content
  • Advertisement
Sign in to follow this  
miki151

Registering a std::function

This topic is 1562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

Complete AngelScript newbie here. I'm in need of emulating a global function with a std::function. I noticed a brief mention of this in the docs, but couldn't find any example.

 

Here's what I tried:

function<void(S1)> printFun = [] (S1 s) { print(s);} ;
engine->RegisterGlobalFunction("void print(S1)", asFUNCTION(printFun), asCALL_THISCALL_ASGLOBAL);

And the error was:

(0,0) : ERROR: Failed in call to function 'RegisterGlobalFunction' with 'void print(S1)' (Code: -24)

 

Share this post


Link to post
Share on other sites
Advertisement

Ok, got it.

engine->RegisterGlobalFunction("void print(S1)", asMETHOD(decltype(printFun), operator()), asCALL_THISCALL_ASGLOBAL, &printFun)

EDIT: it would be useful if one could register the std::function by passing it by value, not by pointer. Right now the function needs to be declared in the same scope as the engine, which kind of defeats the purpose of lambdas.

Edited by miki151

Share this post


Link to post
Share on other sites

I'm glad you figured out how to do it by yourself. I'll see if I can add an example in the documentation for future references.

 

I will not be able to use std::function as part of the core interface. One of the goals with the library to be as portable as possible, and this means that I cannot rely on C++11 features (since older compilers would then be excluded). Internally I don't even use any STL containers for the very same reason.

 

However, it should be quite easy for you to write a simple wrapper for RegisterGlobalFunction that could take a std::function by value and then transform it into the function and object pointer pair that the asIScriptEngine::RegisterGlobalFunction method expects.

Share this post


Link to post
Share on other sites

I'm not sure how I could do that. These functions would need to be stored somewhere during the life of the engine.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!