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

Weird construct behaviour using asMETHODPR

Started by
1 comment, last by WitchLord 10 years, 7 months ago

I'm trying to register a C++ class into angelscript, here's the deal.

I MUST pass a value through the constructor into the class to do that I need to have the factory method be in my scriptingmanager class that looks like this


Image* ScriptingManager::ImageFactory(std::string& mFilename) {
    return new Image(mFilename, this->m_pAFEngine->GetDriver());
}

ok the line that throws the error is this one.


r = this->m_pEngine->RegisterObjectBehaviour("Image", asBEHAVE_FACTORY, "Image f(string &in)", asMETHODPR(ScriptingManager, ImageFactory, (std::string&), Image*), asCALL_CDECL); assert(r >= 0);

with the error being


Message: Failed in call to function 'RegisterObjectBehaviour' with 'Image' and '
Image f(string &in)' (Code: -24) , On Line: 0Assertion failed: r >= 0, line 102

at this point i've tried these calls


r = this->m_pEngine->RegisterObjectBehaviour("Image", asBEHAVE_FACTORY, "Image@ f(string filename)", asMETHODPR(ScriptingManager, ImageFactory, (std::string), Image*), asCALL_CDECL); assert(r >= 0);

r = this->m_pEngine->RegisterObjectBehaviour("Image", asBEHAVE_FACTORY, "Image@ f(string &in)", asMETHODPR(ScriptingManager, ImageFactory, (std::string&), Image*), asCALL_CDECL); assert(r >= 0);

However when I call it like this


r = this->m_pEngine->RegisterObjectBehaviour("Image", asBEHAVE_FACTORY, "Image f(string &in)", asFUNCTIONPR(ImageFactory, (std::string&), Image*), asCALL_CDECL); assert(r >= 0);

All is well, unfortunatly that solution will not work for me as things need to be passed from the class into it.

Advertisement

asBEHAVE_FACTORY cannot be implemented as a method, it must be implemented as a global function. If you have only a single ScriptingManager, you can probably implement it as asCALL_THISCALL_ASGLOBAL. If you have multiple ScriptingManager instances, you'll need to move this factory behavior to be a normal method on that class.

The error -24 means asWRONG_CALLING_CONV, and is because you're telling AngelScript that you're registering a function with the asCALL_CDECL calling convention but in reality you're registrering a class method, which uses the asCALL_THISCALL calling convention.

Like ThyReaper said. As you want to use a class method of your ScriptingManager for the object factory you need to use the asCALL_THISCALL_ASGLOBAL calling convention, and inform the pointer of your ScriptingManager when registering the factory behaviour.

Also, the correct function declaration for the factory is "Image @f(string &in)" or better "Image @f(const string &in)". Both will work in your case, but the latter will allow AngelScript to avoid making a copy of the argument in some cases.


r = this->m_pEngine->RegisterObjectBehaviour("Image", 
                                             asBEHAVE_FACTORY, 
                                             "Image @f(const string &in)", 
                                             asMETHODPR(ScriptingManager, ImageFactory, (std::string&), Image*), 
                                             asCALL_THISCALL_ASGLOBAL, 
                                             this); 
assert(r >= 0);

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