Advertisement

AngelScript: Problem returning a const reference from a class property

Started by December 08, 2016 02:54 PM
3 comments, last by GuyWithBeard 7 years, 11 months ago

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.

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.

Advertisement

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.

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; }

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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!

This topic is closed to new replies.

Advertisement