Instantiating the script class problem

Started by
1 comment, last by IronHawk 7 years, 1 month ago

Hello, Andreas!

I found a strange problem with instantiating the script class from C++.

I'm getting the constructor of the required class using this function:


asIScriptFunction* getConstructorByDeclaration(
    asITypeInfo *pTypeInfo, const std::string& sDeclaration) {

    int nCount = pTypeInfo->GetBehaviourCount();
    for (int i = 0; i < nCount; i++) {
        asEBehaviours nBehaviour;
        asIScriptFunction *pFunction =
            pTypeInfo->GetBehaviourByIndex(i, &nBehaviour);
        if (pFunction != nullptr && nBehaviour == asBEHAVE_CONSTRUCT) {
            if (pFunction->GetDeclaration(false) == sDeclaration) {
                return pFunction;
            }
        }
    }

    return nullptr;
}

And I'm trying to construct an instance of the class like this one:


namespace test
{

// Item
//
//
//
class Item : ItemParent {
    private string m_sZZZ;

    Item(Vector &in v, const string &in sName, const string &in sValue = "") {
        ...
    }
    ...
}

}

But on calling the constructor the context->Execute() exits with an asEXECUTION_EXCEPTION.

Exception is "Null pointer access" and it's occurred at 9:17 in function "Item".
But 9:17 is a line

private string m_sZZZ;

in the Item class.

So it seems that the Execute() routine doesn't like the string variable declaration in class!

If I move this declaration to parent class (ItemParent in my example) an exception is generated in ItemParent.

To be sure I checked the code in AS_DEBUG folder and it shows that 9:17 is actually a variable declaration (if I understood the code properly).


Item::Item(Vector&in, const string&in, const string&in = "")

Temps: 11, 21, 31, 41, 45

Variables:
 -002: Vector& v
 -004: const string& sName
 -006: const string& sValue
 010: string sRealname
 000: Item this
 021: string {noname}
 031: string {noname}
 041: string {noname}
 045: Vector {noname}


- 9,17 -
               VarDecl  0
               VarDecl  1
               VarDecl  2
    0  45 *    SUSPEND
    1  45 *    PshVPtr  v0
    2  47 *    ADDSi    v212, 134217821
    4  47 *    ALLOC    0x463ad90, 20             (type:string, string::string())
...

When I construct instances of these classes (Item or ItemParent) from the script code - all works properly.
I checked the parameters I pass to constructor by moving string variable to ItemParent and printing parameters in Item before calling parent constructor: all params are OK, but exception is still here.

I suspect that something is wrong inside the AS library.

Many thanks!

Advertisement

Instead of trying to call the constructor directly, you should be calling the factory functions (asBEHAVE_FACTORY).

Manual: Instantiating script classes

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

Thank you very much, Andreas!

I saw this topic in manual but thought that the result is the same for both calls (because script function is returned in both cases).

My mistake.

Also maybe it's good to generate an exception like "Unable to call the constructor directly" if someone tries the same as I did?

Because now it's quite hard to understand what's happening.

This topic is closed to new replies.

Advertisement