🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Failure in CreateScriptObject when adding class instance to dictionary

Started by
0 comments, last by WitchLord 9 years, 10 months ago

class foo {}

class FooAndData
{
    FooAndData ( foo@ _foo )
    {
        @memberVar = _foo;
    }

    foo@ memberVar;

    // todo: add more stuff
};

void main()
{
    dictionary dict = { {'entry', foo()} }; // OK
    dict['additionalEntry'] = foo();        // OK
    @dict['additionalEntry'] = foo();       // OK

    dictionary dict2 = { {'entry', FooAndData(null)} }; // OK
    //dict2['additionalEntry'] = FooAndData(null);      // "Failed in call to function 'CreateScriptObject' (Code: -6)"
    @dict2['additionalEntry'] = FooAndData(null);       // OK
}

I'm confused by the failure of the middle call in the second group. Why can I instantiate a parameter-less instance of foo and add it to the dictionary, but I cannot instantiate FooAndData(null) and add it to the dictionary? In my mind, the dictionary is logically storing object instances, not handles to object instances.

Advertisement

    dictionary dict = { {'entry', foo()} };  // This stores the foo instance by value
    dict['additionalEntry'] = foo();          // This copies of the foo instance (value assignment)
    @dict['additionalEntry'] = foo();       // This stores a handle to the foo instance (handle assignment)

The CreateScriptObject fails for FooAndData because the class doesn't have a default constructor and opAssign method or copy constructor (not included automatically since you implemented a non-default constructor).

When initializing the dictionary with the initalization list the dictionary was able to bypass this by storing the actual FooAndData instance, since it was a temporary and wouldn't be used elsewhere.

I'll see if I can come up with a better error message. Unfortunately I don't think it will be possible to detect the error during compile time, since the compiler doesn't know what the dictionary will do with the class on the opAssign call.

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

This topic is closed to new replies.

Advertisement