Problems binding a class

Started by
5 comments, last by SomeFusion 18 years, 10 months ago
I hope this hasn't come up before but since the search is disabled and the Google search didn't find anything I'll just post it. I'm currently trying to bind some of Ogre3D.net classes to AngelScript. But now I'm stuck at registering the Vector3 class. Here's a link to the Vector3 source in Ogre's CVS. I'm trying to register the overloaded operator+ but always get a -10 returned wich is a asINVALID_DECLARATION. But I can't figure out what I'm doing wrong. The operator= works fine. Now here's the code I'm using at the moment:

	int r;
	r = mEngine->RegisterObjectType("Real", sizeof(Real), asOBJ_FLOAT);
	assert(r >= 0);
	r = mEngine->RegisterObjectType("Vector3", sizeof(Vector3), asOBJ_CLASS);
	assert(r >= 0);
	r = mEngine->RegisterObjectBehaviour("Vector3", asBEHAVE_ASSIGNMENT, "Vector3 &f(const Vector3& in)", asMETHODPR(Vector3, operator =, (const Vector3&), Vector3&), asCALL_THISCALL); 
	assert(r >= 0);
	r = mEngine->RegisterObjectBehaviour("Vector3", asBEHAVE_ADD, "Vector3@ f(const Vector3 &in)", asMETHODPR(Vector3, operator +, (const Vector3&) , Vector3&), asCALL_THISCALL);
	assert(r >= 0); // it fails here with -10


Btw, is there any IRC channel for AngelScript? For quick questions it fits perfectly :). I've looked at the ghoul engine source which is a very impressive engine. But I see its using still version 1.7.0 of AngelScript, is it still viable as a reference how to use AngelScript? The registering part looks much easier with that version. Thanks in advance!
Advertisement
Your problem is that you're trying to return the new Vector3 as a handle, but the Vector3 type doesn't support object handles (since it doesn't have the asBEHAVE_ADDREF and asBEHAVE_RELEASE behaviours).

You seem to have implemented the operator+ to return a reference, so you should register the AngelScript behaviour to return a reference as well.

There is no IRC channel for AngelScript, at least not that I'm aware of. I believe that agrav8ed (Dan Royer) created one once, but I don't remember it's name, nor do I know if it still exists.

Using the Ghoul engine as reference is probably not a good idea, a lot have changed since version 1.7.0. You can however use my Texture Generator or the samples that come with the sdk in the sample directory (the apps in the test directory are not very good as samples).



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

Thanks for your help! But I've got a new problem :)

inline Vector3 operator + ( const Vector3& rkVector ) const{    Vector3     kSum.x = x + rkVector.x;    kSum.y = y + rkVector.y;    kSum.z = z + rkVector.z;    return kSum;}


This is how its defined in the Vector3 class. It is returning the Vector3 by value, not by reference.

I have changed the way I register the method now to this:
r = mEngine->RegisterObjectBehaviour("Vector3", asBEHAVE_ADD, "Vector3 f(const Vector3 &in)", asMETHODPR(Vector3, operator +, (const Vector3&) , Vector3), asCALL_THISCALL);


It is not returning -10 anymore but now the assert in as_scriptengine.cpp at line 1050 if failing.
Is it because the in Vector3 is passed by reference? I can't add the AddRef and Release behaviours because I have no control over the Ogre source and don't want to fork my own Ogre to add the methods to the classes I want to bind. Maybe I'm understanding something not right?

If I create #angelscript at freenode would anybody actually come in the channel?
I didn't notice this before, but you're trying to register the asBEHAVE_ADD behaviour as an object method and AngelScript doesn't support this yet. Dual operators must be registered as global behaviours.

You'll have to write a slightly different operator for this:

// NOTE: This may conflict with your class method, if so just just a function name, e.g Vector3_AddVector3 operator+(const Vector3 &a, const Vector3 &b){  return a + b;}...r = mEngine->RegisterGlobalBehaviour("Vector3", asBEHAVE_ADD, "Vector3 f(const Vector3 &in, const Vector3 &in)", asFUNCTIONPR(operator+, (const Vector3&, const Vector3&) , Vector3), asCALL_CDECL);


There is no problem with you not registing the ADDREF and RELEASE behaviours. AngelScript will continue to work just fine. The only difference is that the object type won't support object handles. Basically the type will work as a normal primitive type, except that it can have properties and methods.

If you want anybody to enter the #angelscript channel at freenode, you'll have to announce it. You can do so by making a post in this forum. If the IRC channel catches on, I'll mention it on the angelscript site.

I personally don't have time to enter any IRC channel, so if you want my help, you'll have to continue posting here, or optionally send me an e-mail. I check both with the same frequency, which is 3-5 times a day.

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

It still doesn't work :(. I'm sorry to bother you with this. The method RegisterGlobalBehaviour doesn't take the datatype argument ("Vector3"). But that isn't the problem. I have taken your funtion for overloading and renamed it
since it like you said conflicted with my class method. This is how I register it now:

r = mEngine->RegisterGlobalBehaviour(asBEHAVE_ADD, "Vector3 f(const Vector3 &a, const Vector3 &b)", asFUNCTIONPR(Vector3_Add, (const Vector3&, const Vector3&), Vector3), asCALL_CDECL);

But it returns -10. I think it means asINVALID_DECLARATION. Maybe its a bug in AS? But I think the bug is somewhere in my brain right now :).
hehe, sorry about that. Of course, the datatype shouldn't be passed to the RegisterGlobalBehaviour() method.

You're almost there. [wink] This time you changed the in keyword to a and b parameter names. For parameter references you must specify the direction of the value with either in, out, or inout.

r = mEngine->RegisterGlobalBehaviour(asBEHAVE_ADD, "Vector3 f(const Vector3 &in a, const Vector3 &in b)", asFUNCTIONPR(Vector3_Add, (const Vector3&, const Vector3&), Vector3), asCALL_CDECL);


There is no need to specify the names of the parameters when registering the functions, but it isn't an error to do so either.

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

WOHOOOO! It works, thank you :)

This topic is closed to new replies.

Advertisement