Advertisement

Problem with a function of C++ object which returns a delegate to the AngelScript

Started by June 22, 2016 11:29 AM
2 comments, last by WitchLord 8 years, 5 months ago

Hello!

I experience a problem with a function of C++ object which returns a delegate to the AngelScript.

It works only if I use an &out parameter (like this "getCB(CB_FUNC@ &out) const").

But such variant: "CB_FUNC@ getCB() const" is always returning null.

More clearly:

I registered a funcdef like this:

res = engine->RegisterInterface("IParam");
assert(res >= 0);

res = engine->RegisterFuncdef("void CB_FUNC(IParam@ hParam)");
assert(res >= 0);

Then in my C++ class I implemented two functions and registered them as is:

res = engine->RegisterObjectMethod("CTest",
"void addCB(CB_FUNC@ h)",
asMETHOD(CTest, addCB), asCALL_THISCALL);
assert(res >= 0);
res = engine->RegisterObjectMethod("CTest",
"void getCB(CB_FUNC@ &out) const",
asMETHOD(CTest, getCB), asCALL_THISCALL);
assert(res >= 0);

Function signatures in C++ are:
void CTest::addCB(asIScriptFunction *p);
void CTest::getCB(asIScriptFunction** p) const;

And the test code in AngelScript is:

class ASTest {
CTest test;

ASTest() {
// Save funcdef on C++ side
CB_FUNC@ h = CB_FUNC(this.onCB);
test.addCB(@h);

// And get it back
CB_FUNC@ h2;
test.getCB(@h2);

if(@h2 !is null) {
print("Handle is alive!\n");
} else {
print("Lost my handle.\n");
}
}

void onCB(IParam@ hParam) {
}
}

With all code as above it prints me "Handle is alive!" as expected.

But when I change the C++ function to

asIScriptFunction* CTest::getCB() const;

then register it as

"CB_FUNC@ getCB() const"

and call it as

CB_FUNC@ h2 = test.getCB();

It prints "Lost my handle." which's definitely wrong.

P.S. Of course in both cases I call AddRef()at C++ side before returning the delegate.

P.P.S. The code may contain some typos as I wrote it directly in my Web browser.

Many thanks!

Is this with version 2.31.0?

Have you tried the latest WIP version? Several bugs related to function pointers crept into version 2.31.0, but all of them have been fixed in the WIP version already (including a bug that prevented returning function pointers from 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

Advertisement

Hello, Andreas!

Yes, this was for 2.31.0.

Now I finally updated my version to the latest one.

Thanks.

Thanks for the confirmation.

I'll release version 2.31.1 soon so everyone can take advantage of the latest bug fixes.

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