Advertisement

Support for template functions is now implemented in 2.38.0 WIP

Started by August 18, 2024 01:29 AM
5 comments, last by WitchLord 2 months, 3 weeks ago

I've merged @mindoftony 's contribution for template functions into the WIP version now.

https://sourceforge.net/p/angelscript/code/2943/

For now it is just basic support, but the happy path is fully working, i.e. registering the template function/method and calling it from the script. There is still a lot that needs to be implemented and tested to make it feature complete. I'll be working on those things throughout the coming months before 2.38.0 is released.

(original thread: https://www.gamedev.net/forums/topic/717021-implementing-new-feature-template-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

I finally got the chance to test this, and found at least one issue: global functions in namespaces are not recognized as functions. I registered this function in a namespace called Dev:

void Test<class T>(T v)

(I don't think the class keyword is needed there, but I get the same results either way. I added it after seeing it written in the linked thread.)

Then in my script:

void Main()
{
	Dev::Test<int>(2);
}

And I get this error:

Identifier 'Test' is not a data type in namespace 'Dev' or parent

Walking through the code, it looks like the type is registered correctly, it's just not found when it's compiled.

I'll be doing some more testing with this soon, thank you for merging this! 👍

Edit: Another issue: if I don't have any return value or parameter, I can't find out what type ID was passed in the template from asIScriptGeneric. It's an edge case and an unlikely situation, but probably worth it for someone!

Advertisement

Thanks for reporting this. Yes, there is definitely a lot more work to do to fully support the template functions.

Keep reporting the issues so I can root out as many defects as possoble before the release.

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

I've fixed both issues now.

https://sourceforge.net/p/angelscript/code/2965/

To get the types for the template function you can go to the asIScriptFunction and use the GetSubType/Count/Id methods. This will work even if the template function doesn't have any arguments or return value.

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

I've found another issue. I have a script function with the declaration T Test<T>(T v), and a print function with the declaration void print(?&in) (with C++ parameters void* p, int typeId).

Test is implemented like this:

static void ScriptTestGen(asIScriptGeneric* gen)
{
	gen->SetReturnDWord(gen->GetArgDWord(0));
}

Calling it from scripts now breaks things, as the typeId in the print function becomes the value of the argument passed to Test. For example:

auto n = Test<int>(10);
print(n); // This works and will print 10.

print(Test<int>(10)); // This forces typeId to be 10, and the print function fails

Thanks. I'll review this.

It doesn't appear to be related to the template function implementation, it might just be a pre-existing bug.

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