Skip to main content
gamedev.net
🔒 Locked

problem in registering class

Started by Pooya65 5 replies 1.3k views
Original Post
Pooya65
Pooya65
hi

how can i register my class:

class code:
class vec2f{public:	vec2f (){x = 0 ; y = 0;};	float x;	float y;	float getLen(){ return abs(sqrt(x*x + y*y)); };	void constructor (void* p){new(this) vec2f();};	void destructor(void*p ){}};


and my registration code:
pScriptEngine->RegisterObjectType("vec2f", sizeof(vec2f), asOBJ_APP_CLASS );pScriptEngine->RegisterObjectBehaviour("vec2f", asBEHAVE_CONSTRUCT, "void f()", asMETHOD(vec2f, constructor), asCALL_CDECL_OBJLAST);pScriptEngine->RegisterObjectBehaviour("vec2f", asBEHAVE_DESTRUCT, "void f()", asMETHOD(vec2f, destructor), asCALL_CDECL_OBJLAST);pScriptEngine->RegisterObjectProperty("vec2f","float x",offsetof(vec2f,x));pScriptEngine->RegisterObjectProperty("vec2f","float y",offsetof(vec2f,y));pScriptEngine->RegisterObjectMethod("vec2f","float getLen()" ,asMETHOD(vec2f,getLen), asCALL_THISCALL);
SiCrane
SiCrane
You might want to look at the scriptmath3d add-on for an example of how to register that kind of type.
pifor106
pifor106
First, you should register this class as a POD type. That will save you the constructor call. You are also missing the asAPP_VALUE flag.

Then, your constructor should be a static method, or a global function, but I'm not sure that a member function is really a good idea conceptually ... In your case, the call convention is set a a obj last when in fact you use a this call. I think you are going to see some crashes with that setup.

As a side note, you should use the constructor initialisation list instead of setting the values of your object within the body of the constructor as it is more optimal to do so ( since the compiler will generate the call to the default constructor of the object's aggregated values. In your case, that ain't much because those are floats, but with other types of objects, that may be a unnecessary overhead.

WitchLord
WitchLord
You're declaring the constructor/destructor wrappers as class members. But you're registering them as global functions.

Either declare them as static members, or move them to global scope.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Pooya65
Pooya65
can u help me with sample code of this class?
arpeggiodragon
arpeggiodragon
Here's how I have mine:

	const std::string vec2 =  "Vector2";	r = engine->RegisterObjectType( vec2.c_str(), sizeof(Vector2), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA); assert( r >= 0 );	/* members */	r = engine->RegisterObjectProperty( vec2.c_str(), "float x", offsetof(Vector2, x)); assert( r >= 0 );	r = engine->RegisterObjectProperty( vec2.c_str(), "float y", offsetof(Vector2, y)); assert( r >= 0 );	/* constructors */	r = engine->RegisterObjectBehaviour( vec2.c_str(), asBEHAVE_CONSTRUCT,  "void f()",                     asFUNCTION(Vector2DefaultConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );	r = engine->RegisterObjectBehaviour( vec2.c_str(), asBEHAVE_CONSTRUCT,  "void f(const Vector2 &in)",    asFUNCTION(Vector2CopyConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );	r = engine->RegisterObjectBehaviour( vec2.c_str(), asBEHAVE_CONSTRUCT,  "void f(float, float)",  asFUNCTION(Vector2InitConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );	/* operator overloads */	r = engine->RegisterObjectMethod( vec2.c_str(), "bool opEquals(const Vector2 &in) const", asMETHODPR(Vector2, operator ==, (const Vector2&) const, bool), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "bool opEquals(const Vector2 &in) const", asMETHODPR(Vector2, operator !=, (const Vector2&) const, bool), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "bool opCmp(const Vector2 &in) const", asMETHODPR(Vector2, operator >, (const Vector2&) const, bool), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "bool opCmp(const Vector2 &in) const", asMETHODPR(Vector2, operator <, (const Vector2&) const, bool), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "bool opCmp(const Vector2 &in) const", asMETHODPR(Vector2, operator >=, (const Vector2&) const, bool), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "bool opCmp(const Vector2 &in) const", asMETHODPR(Vector2, operator <=, (const Vector2&) const, bool), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 &opAddAssign(const Vector2 &in)", asMETHODPR(Vector2, operator +=, (const Vector2&), Vector2&), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 &opSubAssign(const Vector2 &in)", asMETHODPR(Vector2, operator -=, (const Vector2&), Vector2&), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 &opMulAssign(const Vector2 &in)", asMETHODPR(Vector2, operator *=, (const Vector2&), Vector2&), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 &opDivAssign(const Vector2 &in)", asMETHODPR(Vector2, operator /=, (const Vector2&), Vector2&), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 &opAddAssign(float)", asMETHODPR(Vector2, operator +=, (float), Vector2&), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 &opSubAssign(float)", asMETHODPR(Vector2, operator -=, (float), Vector2&), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 &opMulAssign(float)", asMETHODPR(Vector2, operator *=, (float), Vector2&), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 &opDivAssign(float)", asMETHODPR(Vector2, operator /=, (float), Vector2&), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opAdd(const Vector2 &in) const", asMETHODPR(Vector2, operator +, (const Vector2&) const, Vector2), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opSub(const Vector2 &in) const", asMETHODPR(Vector2, operator -, (const Vector2&) const, Vector2), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opMul(const Vector2 &in) const", asMETHODPR(Vector2, operator *, (const Vector2&) const, Vector2), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opDiv(const Vector2 &in) const", asMETHODPR(Vector2, operator /, (const Vector2&) const, Vector2), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opAdd(float) const", asMETHODPR(Vector2, operator +, (float) const, Vector2), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opSub(float) const", asMETHODPR(Vector2, operator -, (float) const, Vector2), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opMul(float) const", asMETHODPR(Vector2, operator *, (float) const, Vector2), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opDiv(float) const", asMETHODPR(Vector2, operator /, (float) const, Vector2), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opAdd(float, const Vector2 &in) const", asFUNCTIONPR(operator +, (float, const Vector2&), Vector2), asCALL_CDECL_OBJLAST); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opSub(float, const Vector2 &in) const", asFUNCTIONPR(operator -, (float, const Vector2&), Vector2), asCALL_CDECL_OBJLAST); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opMul(float, const Vector2 &in) const", asFUNCTIONPR(operator *, (float, const Vector2&), Vector2), asCALL_CDECL_OBJLAST); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 opDiv(float, const Vector2 &in) const", asFUNCTIONPR(operator /, (float, const Vector2&), Vector2), asCALL_CDECL_OBJLAST); assert( r >= 0 );	// Register the object methods	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 normal() const", asMETHODPR( Vector2, 		Normal, (void) const, Vector2), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "float length() const", asMETHODPR( Vector2, 		Length, (void) const, float), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "float length_squared() const", asMETHODPR( Vector2, 		LengthSquared, (void) const, float), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "float distance(const Vector2 &in) const", asMETHODPR( Vector2, 		Distance, (const Vector2 &v) const, float), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "float distance_squared(const Vector2 &in) const", asMETHODPR( Vector2, 		DistanceSquared, (const Vector2 &v) const, float), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "float dot(const Vector2 &in) const", asMETHODPR( Vector2, 		Dot, (const Vector2 &v) const, float), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "float cross(const Vector2 &in) const", asMETHODPR( Vector2, 		Cross, (const Vector2 &v) const, float), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 min(const Vector2 &in) const", asMETHODPR( Vector2, 		Min, (const Vector2&) const, Vector2), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 max(const Vector2 &in) const", asMETHODPR( Vector2, 		Max, (const Vector2&) const, Vector2), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 to_degrees() const", asMETHODPR( Vector2, 		ToDegrees, (void) const, Vector2), asCALL_THISCALL ); assert( r >= 0 );	r = engine->RegisterObjectMethod( vec2.c_str(), "Vector2 to_radians() const", asMETHODPR( Vector2, 		ToRadians, (void) const, Vector2), asCALL_THISCALL ); assert( r >= 0 );

Half of those have not been tested but you get the idea.
WitchLord
WitchLord
Please follow SiCrane's suggestion and look at the scriptmath3d add-on. It shows how to register a class like this (Vector3).

scriptmath3d.h

scriptmath3d.cpp

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.