Jump to content
  • Advertisement
Sign in to follow this  
Fayte

Weird construct behaviour using asMETHODPR

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

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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);
Edited by Andreas Jonsson

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!