Home:ALL Converter>Angelscript class method registration

Angelscript class method registration

Ask Time:2011-08-08T18:07:30         Author:Brian

Json Formatter

I am writing some basic code to experiment with Angelscript, however I cannot get even the simplest functions to work correctly. Here is the basic code block:

class Engine {
    public:
       void print(std::string&);
};

Engine::print(std::string &msg)
{
    cout<<msg.c_str()<<endl;
}

Here is the actual code that initializes and registers C functions for Angelscript:

int r;

asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

r = engine->SetMessageCallback(asMETHOD(Engine,MessageCallback), this,asCALL_THISCALL);
assert( r >= 0 );

RegisterStdString(engine);

r = engine->RegisterObjectType("Engine", 0, asOBJ_APP_CLASS_CONSTRUCTOR);
cout<<"r was: "<<r<<endl;
assert( r >= 0 );

r = engine->RegisterObjectMethod("Engine","void print(const string &in)", asMETHOD(Engine,print), asCALL_THISCALL);
assert( r >= 0 );

At first I did not have the function "RegisterObjectType()" present, so running the program would throw a "'Engine' is not a valid type" error even though it was a valid class. So I found a function called "RegisterObjectType()" and implemented it as above and now it's throwing a "Invalid Flag" error on the 'RegisterObjectType()' function in the last field. I've tried all the class flags and it still throws this error.

What is the proper method for registering a class method into Angelscript? The documentation example didn't seem to work. It seems to snip out everything except the actual function it is explaining, for example, it did not hint about registering object types with the class method registration code. Or it was not very clear about it.

I should mention that all the errors encountered are runtime errors thrown by the angelscript libraries and not compiler errors.

Thanks!

Author:Brian,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6980548/angelscript-class-method-registration
yy