Sign in to follow this  
jackbanion

Temporary array as argument

Recommended Posts

This is my first post. I read the FAQ, but if I'm violating any etiquette, please let me know.

 

I am having trouble with Angelscript syntax. Is there any way to create a temporary array? I'd like to pass one to a class constructor without creating an array variable beforehand. That way the array only exists for the purpose of object construction.

 

I've posted my test class below, along with how I'm currently accomplishing this by declaring an array before the object creation, and also what I'm trying to do.

class MyObj
{
    MyObj(const uint[]@ vals)
    {
        _values = vals;
    }

    const uint[]@ get_values() const
    {
        return _values;
    }

    void set_values(const uint[]@ vals)
    {
        _values = vals;
    }

    uint[] _values;
}

// How I'm doing this now
uint[] input = { 20, 40, 60, 80, 100 };
MyObj instance(input);


// How I would like to do it, if possible
MyObj instance( { 20, 40, 60, 80, 100 } );

 

Share this post


Link to post
Share on other sites

Hi jackbanion. Welcome to the forum :)

 

Anonymous arrays currently require that you specify the type of the initialization list. The compiler is not yet capable of determining the type automatically by looking at what the list is used for.

 

MyObj instance( uint[] = { 20, 40, 60, 80, 100 } );
 
// I'm actually not entirely sure right now if the to-be deprecated syntax uint[] works. If not try this instead
MyObj instance( array<uint> = { 20, 40, 60, 80, 100 } );

Share this post


Link to post
Share on other sites

Thanks for the reply Andreas.

 

I am writing Angelscript code on an embedded display that supports it, but I believe it uses an older version of Angelscript. It doesn't seem to accept the array<uint> = { ... } line or the uint[] = { ... } line. I know it is an older version because there also isn't support for access modifiers (public/private) in the script classes. Or maybe the array<> template existed in this version and the manufacturer didn't register support for it.

 

Do you know of any other way to do this with the non-array<> syntax? When I use uint[] = { ... }, I get a script compile error saying:

 

ERR  : Expected '('
 
 
I think I'm just going to be stuck doing the two-line method.

Share this post


Link to post
Share on other sites

Do you have any idea of which version of AngelScript that is running on the embedded display? Support for anonymous arrays were introduced in version 2.28.2, released on March 18th, 2014.

 

I checked now, the syntax MyObj instance( uint[] = { 20, 40, 60, 80, 100 } ); isn't supported even in the current version. I'll look into adding that.

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