Jump to content
  • Advertisement
Sign in to follow this  
IronHawk

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

This topic is 820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

 

 

Share this post


Link to post
Share on other sites
Advertisement

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)

Share this post


Link to post
Share on other sites

Hello, Andreas!

 

Yes, this was for 2.31.0.

Now I finally updated my version to the latest one.

 

Thanks.

Share this post


Link to post
Share on other sites

Thanks for the confirmation. 

 

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!