Advertisement

Suggestion: Callback for external functions

Started by May 06, 2018 01:56 PM
8 comments, last by Miss 6 years, 6 months ago

I would like to implement loading functions from external DLL's, in a syntax such as this:


[Dll file="Test.dll"]
external void DoSomething();

Currently, the external keyword requires that the function be defined in a module already before this declaration is compiled (since it's (implicitly?) a shared function), which breaks my ability to get the metadata from CScriptBuilder about the function.

My suggestion is to have a callback function for external functions where I can resolve the function manually, possibly referring it to some asCALL_GENERIC function that handles the dll call.

Perhaps this should be implemented in the CScriptBuilder addon rather than the library itself, since that holds the info on the metadata above the declaration.

Any tips/workarounds would be nice to hear as well.

'external' is meant for shared functions from previously compiled script modules, and not for registered functions.

You can still use the syntax you want, but then you need to make modifications to CScriptBuilder to identify these in a pre-processing pass, register the functions, and then build the script module.

However, the '#pragma' directive is probably more suitable for this. The CScriptBuilder in the WIP version supports defining a callback for the pragma directive which is identified during the pre-processing pass. Your callback can then load the dll and register the desired function.

Example:


#pragma use 'void DoSomething()' from 'foo.dll'

I`m planning to do something similar in the asrun sample to support plugins. Currently I only use the pragma directive it to inform asrun that the script should be started in debug mode.

 

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

Advertisement

And maybe we could load library in similar way just like:


#pragma library("foo.dll")

and suppose there is a function "void InitializeLibrary(asIScriptEngine* engine)" in the dll, so we can register types and functions in the function.

Exactly. That's what I'm planning to do in asrun.

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

That would be very nice too. I'd love to have such a thing as an addon if you end up putting it in asrun!

I don't see it being generic enough to be useful as an add-on. However, the code for asrun is also part of the library so you will be free to reuse it as you see fit :)

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

Advertisement

Hmm, the problem with the #pragma approach is that if you have multiple modules, registering a function will be available to each module rather than only 1 module. Is there anything that can be done about that?

If you want different modules to have different application interfaces, then you can do one of the following:

1. use multiple engines

2. use configuration groups with different access masks. You'll need to use namespaces too to resolve conflicts if two modules happen to load the same plugin. 

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, I'll experiment with this.

This topic is closed to new replies.

Advertisement