Advertisement

Importing C++ functions from AS

Started by June 07, 2016 11:09 AM
0 comments, last by WitchLord 8 years, 5 months ago

Hello,

I was thinking, is there a way to import a function from C++ and specify the signature from angelscript?

Like GetProcAddress returns a void* and we cast it to the correct function declaration in C++ to call the function correctly, is there a similar mechanism in AS ?

All I could find was funcdefs but it appears to be a way to declare angelscript call conventions and not C++ call conventions.

Thanks,

Max

What you can do is something like this:

Implement a C++ function with the following logic:

  • Get the desired funcdef signature from the output argument of variable type
  • Determine the address of the matching C++ function
  • Register the C++ function with the script engine
  • Get the asIScriptFunction pointer for the newly registered C++ function
  • Return the asIScriptFunction in the output argument

The most difficult part will be making sure the C++ function actually have the matching signature. Because if the signature is not matching and the script calls the function, you'll most like end up crashing the application due to stack corruption or memory invasions.

If you wish to register C++ functions dynamically like this, then I suggest you look into how to dynamically determine signatures of C++ functions. I've never done this myself, so I cannot give you much help in that direction, but perhaps it would be possible if the C++ function is accompanied with something like IDL that can be read at run time. Or perhaps you could look for some way of demangling the C++ symbols (though this is compiler specific).

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