• Advertisement
Sign in to follow this  

AngelScript: Problem returning a const reference from a class property

This topic is 439 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

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
Advertisement

Try making the property accessor const as well:

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

 

If that doesn't work then properties probably need to be non-const to support setters, even if you don't define a setter.

Share this post


Link to post
Share on other sites

I get the same errors with both of these:

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

and

gameobject& player { get { return mPlayer; } }

So it seems like it is the reference operator (or whatever the '&' is called) that causes the problems.

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

Thanks Andreas, that works. And it's fine for me right now as the LevelObjects class gets generated by the level editor anyways.

 

Stellar work as usual! Cheers!

Share this post


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

  • Advertisement