Sign in to follow this  
GuyWithBeard

AngelScript: Problem returning a const reference from a class property

Recommended Posts

I have this piece of code that uses a c++ side class called "gameobject" which is registered with AS as asOBJ_VALUE:

class LevelObjects
{
    LevelObjects()
    {
        mPlayer = "player";
    }

    const gameobject& getPlayer() { return mPlayer; }

    private gameobject mPlayer;
}

The above code seems to work. At least it compiles. If I try to make the getPlayer method a property I get the following errors:

 

ERROR: Component source code (8, 30): Expected '('

ERROR: Component source code (8, 30): Instead found '{'
ERROR: Component source code (11, 5): Unexpected token 'private'
ERROR: Component source code (12, 1): Unexpected token '}'
 
The problematic version looks like this:
class LevelObjects
{
    LevelObjects()
    {
        mPlayer = "player";
    }

    const gameobject& player { get { return mPlayer; } }

    private gameobject mPlayer;
}

If I remove the "const" and the "&" from the property it does compile, but I assume that creates a new temp copy of gameobject every time I use the property. I would like to avoid doing that.

 

Is this not supported or am I doing something wrong? The version of AS I am using is 2.31.1.

 

Share this post


Link to post
Share on other sites

Unfortunately, I haven't prepared the parser for this scenario. I'll look into seeing if the code can easily be changed to support virtual properties as const references too.

 

 

In the meantime, try declaring the get_player method explicitly instead of using the virtual property syntax.

 

const gameobject &get_player() const { return mPlayer; }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this