Advertisement

Questions about Scriptmath3d

Started by December 09, 2008 08:29 AM
1 comment, last by B_old 15 years, 11 months ago
Hi, I'd like to register my own math with angelscript and tried to get some inspiration from the scriptmath example. Now I have a few questions. Why is this special contstructor function needed and do I need a destructor (or does AngelScript free the allocated memory)? My add and assign operator is working exactly the same way as in the example. So I could copy the provided code. My add operator however is different then yours. It looks like this:

template<typename T>
struct Vector3_t
{
        Vector3_t operator+(const Vector3_t &v) const
	{
		return Vector3_t(x + v.x, y + v.y, z + v.z);
	}
};

I tried to register this operator the same way as add assign, but it didn't really work.

engine->RegisterObjectBehaviour("Vector3", asBEHAVE_ADD, "Vector3 f(const Vector3 &in) const", asMETHODPR(Vector3, operator+, (const Vector3 &) const, Vector3), asCALL_THISCALL); //not working

Is this possible at all or am I making a minor mistake. Thanks for the help (and the nice engine)!
Hi B_old,

the constructor function is necessary because C++ doesn't let you take the address of the class constructors, so there is no other way of telling AngelScript how to call the class constructor.

Of course, if you don't want to register any constructor at all, then it's not necessary. However, in this case the vector's member will start with uninitialized values.

No, the destructor is not necessary, only if the type is allocating resources on its own that it needs to free upon destruction. AngelScript is responsible for allocating and deallocating the memory needed by the type.

AngelScript does not yet accept registration of the dual operators as class methods. They have to be registered as global behaviours using global functions. In your case you'll have to write a basic wrapper function for your member operator and then register it like I've done in the scriptmath3d add-on.

I'll change this in a future version of AngelScript though, so it will be possible to use both global functions and class methods to register the operators.

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

Advertisement
Thanks for the info!
I wondered whether I maybe should change my operators to suit AngelScript, but now decided to just use a wrapper until the operators are supported that way. I suppose it won't hurt.

This topic is closed to new replies.

Advertisement