Jump to content
  • Advertisement
Sign in to follow this  

Instantiating the script class problem

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

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

 -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!



Share this post

Link to post
Share on other sites

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.

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!