🎉 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!

Dynamic loading of function

Started by
5 comments, last by WitchLord 2 years, 10 months ago

Hi !

I want to know if it's possible to dynamically load a function for a script.

Here's a script sample

void main() {
    load_function("cat.dll")
    string result = cat("C:\\Path\\to\\file")
}

What load_function should do is loading a shared library which embeds a cat functionality, then a new function is added to the context so my script can use it.
Is this behaviour possible ? If not, is there a way for me to get a variable containing the wanted function before the execution so I can include them in the module ? (different than just parsing the script manually).

thank you

Advertisement

It is possible, though it will require some coding from your end.

option 1: load at run-time

Basically you would register a function with the library something like this:

ref @load_function(const string &in library, const string &in function);

The function could be implemented to load the dll, find the function address of the cat function, then register the function with the engine and finally return the asIScriptFunction for the registered function in a CScriptHandle add-on.

In the script it would receive this reference and cast it to the appropriate funcdef before calling it. For example:

funcdef string FUNC(const string &in)
FUNC @cat = cast<FUNC>(load_function("cat.dll", “string cat(const string &in)”);

option 2: load at compile time

The CScriptBuilder add-on allows you to implement callbacks for #pragma directives. You could use this to implement a pragma directive that would load the cat dll and register the function with the engine before the script is compiled so it could be used just as any other function. For example:

#pragma load_function("cat.dll", “string cat(const string &in)”)

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

Thank you for your answer.

I understand the option 2 but I'll need to do some experiments to see how it works. If I understood well I'll need to parse pragmaText and see that it's asking the load_function function.

However I don't understand the option 1. There are a few things that I can't get my head around:

  • if I call a C++ function from my AS script, I don't have any handler to the engine, some how can I register a function via RegisterGlobalFunction ?
  • I don't understand the ref @function syntax even after reading the documentation about ref

Could you provide an example on how to do it ? (I don't ask how to load the dll and find the function address, I know how to implement it)

Thank you very much !

You can use asGetActiveContext to obtain the current context, and from that get the engine.

I haven't compiled this, but you should be able to do something like this:

// registered as 'ref @load_function(const string &in library, const string &in function)'
CScriptHandle load_function(const std::string &library, const std::string &funcSignature)
{
   asIScriptContext *ctx = asGetActiveContext();
   asIScriptEngine *engine = ctx->GetEngine();
   
   ... load library and get the function address
   
   int funcId = engine->RegisterGlobalFunction(funcSignature, ...);
   asIScriptFunction *func = engine->GetFunctionById(funcId);
   
   CScriptHandle ref;
   ref.Set(func, func->GetObjectType());
   return ref;
}

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

@witchlord I wanted to try this out too, my problem is that I have multiple scripts running that can register a dll function, but these scripts can be started and stopped dynamically, so when they are stopped the dll function should be unregistered. Is it somehow possible to unregister a previously registered global function?

None

Yes, that is also possible by using configuration groups. These can be removed as long as there are no scripts still referring to the registered functions.

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

This topic is closed to new replies.

Advertisement