Sign in to follow this  

Why I can't register a method which recieved a script class sucessfully?

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

this is my c++ code:

class Base {
public:
    Base() {
        _refCount = 1;
        cout << "Base Create" << endl;
    }

    void addRef() {
        _refCount++;
    }

    void release() {
        _refCount--;
        if (_refCount == 0) {
            delete this;
        }
    }

    void reciever(asIScriptObject *obj) {
        cout << "???" << endl;
        man = obj;
    }

    void active() {
        cout << "??Active" << endl;
        cout << (man == 0) << endl;
        cout << "??Active" << endl;
    }

private:
    int _refCount;
    asIScriptObject *man;
};

Base *Base_Factory() {
    return new Base();
}
......
//---------------------------------------------------------
    scriptEngine->RegisterObjectType("Base", 0, asOBJ_REF);
    scriptEngine->RegisterObjectBehaviour("Base",asBEHAVE_FACTORY, "Base@ f()", asFUNCTION(Base_Factory), asCALL_CDECL);
    scriptEngine->RegisterObjectBehaviour("Base", asBEHAVE_ADDREF, "void f()", asMETHOD(Base, addRef), asCALL_THISCALL);
    scriptEngine->RegisterObjectBehaviour("Base", asBEHAVE_RELEASE, "void f()", asMETHOD(Base, release), asCALL_THISCALL);

    //---------------------------------------------------------
    CScriptBuilder builder;
    r = builder.StartNewModule(scriptEngine, "MyModule");

    int isOK = builder.AddSectionFromFile("init.as");
    asIObjectType *type = scriptEngine->GetObjectTypeByName("Human");
    cout << "type == " << (type == 0) << endl;

    //r = builder.AddSectionFromFile("test.as");

    r = builder.BuildModule();
  
    scriptEngine->RegisterObjectMethod("Base", "void reciever(Human @in)", asMETHOD(Base, reciever), asCALL_THISCALL);
    scriptEngine->RegisterObjectMethod("Base", "void active()", asMETHOD(Base, active), asCALL_THISCALL);
........

and in my init.as script code:

class Human {
	Human(){}
}

but it is still report a error:

Failed in call to function 'RegisterObjectMethod' with 'Base' and 'void reciever(Human @in)' (code: -10)

why ?  may it must to reciever a interface nor a script class?

Share this post


Link to post
Share on other sites
You need to register an interface name from the application first with asIScriptEngine::RegisterInterface(const char *name). Then you will be able to register the Base::receiver method that takes a handle to that interface. http://www.angelcode.com/angelscript/sdk/docs/manual/doc_use_script_class.html#doc_use_script_class_3

Share this post


Link to post
Share on other sites

thanks

but there are something like this in the angelScript mannual:

Of course, since the class is declared in the script it isn't possible to know the type before the script is compiled.

but I can't work it successfully

is this because of angelScript does not support reciever a script class thich declared in the as script file?

Share this post


Link to post
Share on other sites

It can receive it, but you cannot register functions that explicitly refer to the class, because the class type is not known at the time of registering the function. That's why the application should register an interface that the class will then implement. Like this:

 

 

void ReceiveClass(asIScriptObject *obj)
{
}
 
void ConfigureEngine(asIScriptEngine *engine)
{
   // Register an interface that the classes should inherit from
   engine->RegisterInterface("MyIntf");
 
   // Register the function that will receive the classes
   engine->RegisterGlobalFunction("void ReceiveClass(MyIntf @+)", asFUNCTION(ReceiveClass), asCALL_CDECL);
}

The script class is then implemented like this:
 

// AngelScript
class MyClass : MyIntf
{
}
 
void main()
{
   MyClass c;
 
   // Pass the object to the application
   ReceiveClass(c); 
}

Edited by Andreas Jonsson

Share this post


Link to post
Share on other sites

well thanks

but is this means before we register the reciever function

we can add a new section from file A or memory(string)

so that engine can pass the script code and know the script class declared in file A

and when we register the reciever function the engine had know the script class type

I have try this  but it not work 

it's so strange>>>right?

Share this post


Link to post
Share on other sites

Actually, it is not so strange. The types declared in the script modules are in a different scope than the engine registered functions and classes. That is why even though you have compiled a script the types from that script cannot be used when registering engine functions. 

 

The reason for this is that multiple modules can declare the types with the same names without conflict. Modules can also be recompiled or removed dynamically. So if a registered function refer to any of those types, what would happen then?

Share this post


Link to post
Share on other sites

This topic is 1723 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.

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