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!