Advertisement

asBEHAVE_INDEX more then 1 dimension

Started by July 05, 2004 08:32 AM
7 comments, last by zola 20 years, 4 months ago
Hi I was wondering if it is possible to have indexing behaviour with more then one dimension? I have a Matrix class which implements an index operator[] such that i can write the following in c++ Matrix4 m; m[0][3]=1; Is there a way i could make angelscript show a similar behaviour with the registered version of my Matrix4 class? Currently i use some wrappers ( set(row,col,value); get(row,col); ) but this doesn't look good in the code ;) Regards Tom
yes, it's possible, just look at D3DXMATRIX from DirectX SDK
www.tmreality.com
Advertisement
D3DXMATRIX m;

m(0,0) = 1.0f;



www.tmreality.com
Sorry, I don't under stand what You want to say?

I'm aware that D3DXMATRIX has this operator

FLOAT& operator () ( UINT Row, UINT Col );

What I don't know is how I can register the asBEHAVE_INDEX behaviour in AngelScript to use the access operators provided by D3DX or my own class, yet if this would be possible at all?

Maybe I'm overlooking something very obvious, could You elaborate a bit more?

Tom
tomek_zielinski:

It's not quite that simple ;). What Tom is talking about is how to register the indexing operator with AngelScript, not just accessing the elements.

zola:

Well, it would certainly be possible to implement an overloaded operator that take two parameters like this. It shouldn't be much more difficult either. I'll have to think about it, because once implemented I can't remove it again since I try to keep backwards compatibility as much as possible.

The way to implement it today is to let the first indexing operator return a pointer to a matrix row (new registered type), which in turn has an indexing operator to access the elements of that row.

If you register the matrix row type with a size of 0 you prevent the scripts from declaring variables of that type. For C++ the pointer to the matrix row is just a pointer to the first element in the matrix.

Access this way wouldn't be very fast though (two system function calls), but at least the syntax would be what you want.

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

Oh, sorry - my mistake - I didn't see that it was about AngelScript. Sorry once again...
www.tmreality.com
Advertisement
zola:

You could also at least improve your set() and get() pair by a method float &m(int r, int c); which would allow your scripts to write the following:

matrix mtx;

mtx.m(1,1) = 32;
mtx.m(2,1) = 32;

Still not the correct way, but it is closer, don't you think?

I'll think some more about this. After all, C++ has the possibility of overloading the () to make a function call directly. I'll just have to decide if I want AngelScript to have this operator overload or not. The syntax for registering the operator would be:

engine->RegisterTypeBehaviour("matrix", asBEHAVE_INDEX, "float &f(int r, int c)", asMETHOD(matrix, m), asCALL_THISCALL);

Using this from the script would be like

matrix mtx;
mtx[r][c] = 1;

But as I said, I'll decide if this is something AngelScript will have (though it probably will as I don't see any real reason not to :))

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

Just thought of something that prevents me from implementing multilevel indices like this.

Suppose we register two index operators [](int r), and [](int r, int c), where the first one returns an object that also has an index operator, e.g. matrix row. This would create an ambiguity for the compiler, that could only be solved with parenthesis.

For now, I think I'll keep it the way it is. At least until native support for arrays are available.

The best implementation would then be:

engine->RegisterObjectType("matrix4", sizeof(matrix4), 0);engine->RegisterObjectType("matrix4_row", 0, 0);engine->RegisterTypeBehaviour("matrix4", asBEHAVE_INDEX, "matrix4_row &f(int)", asFUNCTION(matrix4_row), asCALL_CDECL_OBJLAST);engine->RegisterTypeBehaviour("matrix4_row", asBEHAVE_INDEX, "float &f(int)", asFUNCTION(matrix4_col), asCALL_CDECL_OBJLAST);


float &matrix4_row(int r, matrix4 &mtx){  if( r < 0 || r > 3 )     // set exception on the active context  return &mtx[r][0];}float &matrix4_col(int c, float *col){  if( c < 0 || c > 3 )     // set exception on the active context  return &col[c];}


I think the above code should work. But I haven't tried it, nor compiled it so you should only use it as a guide for your own implementation.

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

Hi WitchLord

Thank You for the hints.

I will use the method with the function returning a reference to the indexed value, this seems to be a resonable way.

Cheers
Tom

This topic is closed to new replies.

Advertisement