Advertisement

Registering operators

Started by April 13, 2008 04:58 PM
6 comments, last by WitchLord 16 years, 7 months ago
I'm having some trouble getting basic operators registered into AngelScript. Basically I'm looking to do standard operators such as operator*, operator+, and so on. Everytime I try and register it fails. Heres my operators Point2F operator + ( const Point2F& ) const; Point2F operator - ( const Point2F& ) const; Point2F operator * ( const float ) const; Point2F operator / ( const float ) const; void operator = ( const Point2F& ); And heres how I'm registering things. The bottom four commented out lines are the operators that I'm trying to register.

//============================================================
void Point2F::initScript( ScriptManager *script )
{
	//Make this a scoped reference type
	script->GetASEngine( )->RegisterObjectType( "Point2F", 0, asOBJ_REF | asOBJ_SCOPED );
	script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_FACTORY, "Point2F @f()", asFUNCTION( Point2FConstructor ), asCALL_CDECL );
	script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_FACTORY, "Point2F @f( float, float )", asFUNCTION( Point2FConstructorOverloaded ), asCALL_CDECL);	
	script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_RELEASE, "void f()", asFUNCTION( Point2FDestructor ), asCALL_CDECL_OBJLAST);

	//Properties
	script->GetASEngine( )->RegisterObjectProperty( "Point2F", "float x", offsetof(Point2F,x));
	script->GetASEngine( )->RegisterObjectProperty( "Point2F", "float y", offsetof(Point2F,y));
	script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_ADD_ASSIGN, "Point2F &f( const Point2F ∈ )", asMETHOD( Point2F, operator += ), asCALL_THISCALL );
	script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_SUB_ASSIGN, "Point2F &f( const Point2F ∈ )", asMETHOD( Point2F, operator -= ), asCALL_THISCALL );
	script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_MUL_ASSIGN, "Point2F &f( const float )", asMETHOD( Point2F, operator *= ), asCALL_THISCALL );
	script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_DIV_ASSIGN, "Point2F &f( const float )", asMETHOD( Point2F, operator /= ), asCALL_THISCALL );
	//script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_ADD, "Point2F f( const Point2F ∈, const Point2F ∈ )", asFUNCTION( Vector2Add ), asCALL_CDECL );
	//script->GetASEngine( )->RegisterGlobalBehaviour( asBEHAVE_ADD, "Point2F f( const Point2F ∈ )", asFUNCTION( Point2F, operator + ), asCALL_CDECL );
	//script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_SUBTRACT, "Point2F f(Point2F ∈, Point2F ∈)", asMETHOD( Point2F, operator - ), asCALL_THISCALL );
	//script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_MULTIPLY, "Point2F f( const float )", asMETHOD( Point2F, operator * ), asCALL_THISCALL );
	//script->GetASEngine( )->RegisterObjectBehaviour( "Point2F", asBEHAVE_DIVIDE, "Point2F f( const float )", asMETHOD( Point2F, operator / ), asCALL_THISCALL );

	//Methods
	script->GetASEngine( )->RegisterObjectMethod( "Point2F", "void normalize()", asMETHOD( Point2F,normalize ), asCALL_THISCALL );
	script->GetASEngine( )->RegisterObjectMethod( "Point2F", "float len( )", asMETHOD( Point2F, len ), asCALL_THISCALL );
	script->GetASEngine( )->RegisterObjectMethod( "Point2F", "float dot( const Point2F ∈ )", asMETHOD( Point2F, dot ) , asCALL_THISCALL );
}
Thanks in advance Adam
Adamhttp://www.allgamedevelopment.com
What kind of problem are you having? Does the C++ code not compile? Does AngelScript return a failure when you run the program?
Advertisement
You have to define the operators outside of the class.
The math operators must be registered as global operators. For this you must have global functions that take 2 parameters. For example:

Point2F Point2F_Add(const Point2F &a, const Point2F &b){  return a + b;}script->GetASEngine( )->RegisterGlobalBehaviour( asBEHAVE_ADD, "Point2F f( const Point2F ∈, const Point2F ∈ )", asFUNCTION( Point2F_Add ), asCALL_CDECL );


Assignment operators (including compound assignments) must be registered as object behaviours though, as these modify the actual object.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

After spending another hour trying to get this to work it's still failing out with this error.

(0, 0) : ERR : Invalid configuration

I've tried with asFUNCTION and asFUNCTIONPR

//============================================================Point2F Vector2Subtract( const Point2F &left, const Point2F &right ){	return left - right;}........	script->GetASEngine( )->RegisterGlobalBehaviour( asBEHAVE_SUBTRACT, "Point2F f( Point2F ∈, Point2F ∈ )", asFUNCTION( Vector2Subtract ), asCALL_CDECL );



So basically to give you some back story on whats happening. I built an entire Math Library that could be accessed by Script using a version of AngelScript from about a 8 months ago. With some of the newer releases most of it quit working so I'm trying to get it back up to date. In my older version I did do the global functions for math operators, but whats strange is no matter what way I try now it's failing.

It's probably something stupid on my part.

Thanks again for all the assistance.
Adam
Adamhttp://www.allgamedevelopment.com
Try registering the message callback before you register your math library, and add asserts on every function registration. That should help you pinpoint the problem.

r = engine->RegisterXXX(); 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

Advertisement
Its really the strangest problem i've had with AngelScript so far.

The line

script->GetASEngine( )->RegisterGlobalBehaviour( asBEHAVE_ADD, "Point2F f( const Point2F ∈, const Point2F ∈ )", asFUNCTION( Point2F_Add ), asCALL_CDECL );

returns -10

Nothing else fails out before that point, and the message callback is registered right away and just prints out the invalid configuration error that I posted above.

I've tried a ton of different configurations and none of them seem to work.


Here is the old way I use to register things using a wrapper.

int ScriptManager::RegisterGlobalBehavior(std::string decl, asUPtr funcPointer, asDWORD behavior){	int hret = m_scriptEngine->RegisterGlobalBehaviour(behavior, decl.c_str(), funcPointer, asCALL_CDECL);	return hret;}... Heres some of my existing Point3F class which use to work fine, but now has the same problem my Point2F class has.	script->RegisterGlobalBehavior("Point3F f(Point3F ∈, Point3F ∈)", asFUNCTION(Vector3Add), asBEHAVE_ADD);	script->RegisterGlobalBehavior("Point3F f(Point3F ∈, Point3F ∈)", asFUNCTION(Vector3Sub), asBEHAVE_SUBTRACT);	script->RegisterGlobalBehavior("Point3F f(Point3F ∈, float)", asFUNCTION(Vector3Mul), asBEHAVE_MULTIPLY);	script->RegisterGlobalBehavior("Point3F f(Point3F ∈, float)", asFUNCTION(Vector3Div), asBEHAVE_DIVIDE);



I'll keep hammering on it, but it almost seems that these behaviors don't work if I register my object like this.

script->GetASEngine( )->RegisterObjectType( "Point2F", 0, asOBJ_REF | asOBJ_SCOPED );

Because I use to register it as asOBJ_APP_CLASS_CDA.

Could that be the problem?
Thanks Again
Adam



Adamhttp://www.allgamedevelopment.com
That is indeed the problem.

A class registered as asOBJ_REF can't be returned by value from application registered functions, nor can they be passed by value to the application registered functions.

This is because an asOBJ_REF lives on the heap, and is allocated via the factory behaviour, thus cannot be placed on the application stack.

Either you'll have to change your behaviours to return an Point2F by handle, or register the object with asOBJ_VALUE instead.

I'll see if I can have AngelScript emit a better error message in this situation.

Regards,
Andreas

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