arrays and handles question..

Started by
6 comments, last by AlphaDog 18 years, 10 months ago
First let me say that I am really enjoying working with AngelScript... I'm having trouble understanding how best to pass an array of objects to a script defined function when I want to pass the array as a handle. If that is possible.

// My objects are referenced counted and if I create an array like this:
// AngelSctipt
MyObject[] objects(10);
for( int t = 0 ; t < 10; t++ )
{
     // set an internal int val
     objects[t].val = t;
}

// And I pass these objects to a function like so:
// AngelScript
add(@objects);

// And the script function is defined like this:
//AngelScript
void add(MyObject[] @objs)
{
     for(int t = 0 ; t < 10; t++ )
     {
          objs[t].add(t);
     } 
}

// My class member function looks like this:
// C++
void CMyObject::add( int t )
{
     val += t;
}




If I pass by reference or pass by value, my objects get constructed and destructed, which is something I don't want. So I would like to use handles. But if I pass the array by handle my app crashes. What am I missing.. Thanks, Scott [Edited by - AlphaDog on June 9, 2005 7:45:26 PM]
[size=1]'Behold! It is not over unknown seas but back over well-known years that your quest must go; back to the bright strange things of infancy and the quick sun-drenched glimpses of magic that old scenes brought to wide young eyes.'
Advertisement
First let me say that I am really pleased that you enjoy working with AngelScript... [grin]

Let's see...

Passing arrays between application and scripts is a complicated matter (I'm working on making it at least a little easier) because the native C++ arrays are not compatible with AngelScript's arrays.

In order for you to be able to pass an array from the application to the script you have to first register a new object type overriding AngelScript's built-in implementation for that array type. Did you do this?

If you did do everything correctly up to this point, then my guess is that the application crashes because you didn't increase the reference count for the handle to the array that you sent to the script function.

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

Actually I'm not passing arrays between application and scripts. I'm only passing them between functions in scripts. Problem is when I iterate over the array of my objects that was passed in by handle in the called function and call a method on the object that modifies a property of that class, it crashes. I noticed by dumping out the address of the this pointer in the method call that the address is of an object that has never been constructed. I edited my fist post to notate what is AngelScript and what is C++.

Scott

[Edited by - AlphaDog on June 10, 2005 8:44:11 AM]
[size=1]'Behold! It is not over unknown seas but back over well-known years that your quest must go; back to the bright strange things of infancy and the quick sun-drenched glimpses of magic that old scenes brought to wide young eyes.'
Oh, my mistake.

Well, then it seems that you may have found a bug. What version of AngelScript are you using?

I'll do some tests to see if I can figure out what is going on.

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

I found the bug. The compiler wasn't clearing the flag that said the type is an object handle when determining the type of the array element.

To fix this you need to substitute the implementation for asCDataType::GetSubType() to:

asCDataType asCDataType::GetSubType() const{	asCDataType dt(*this);	dt.arrayType >>= 2;	dt.isReadOnly = false;	dt.isConstHandle = false;	dt.isReference = false;	if( dt.arrayType == 0 )		dt.objectType = dt.baseType;	else		dt.objectType = objectType->engine->GetArrayType(dt);	dt.MakeHandle((arrayType & 1) ? true : false);		if( IsReadOnly() )		dt.MakeReadOnly(false);	return dt;}


This is assuming that you're using version 2.2.0. I will now check if the same bug exists in version 2.1.0c.

Thanks for discovering the bug.

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

Great.. Thanks for the fix, I'm going to try it out now..

Scott
[size=1]'Behold! It is not over unknown seas but back over well-known years that your quest must go; back to the bright strange things of infancy and the quick sun-drenched glimpses of magic that old scenes brought to wide young eyes.'
I made the same test for version 2.1.0c and I'm pleased to say that the bug didn't present itself in that version.

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

I was using 2.2.0. Just tested and it's working great. Thanks again..
[size=1]'Behold! It is not over unknown seas but back over well-known years that your quest must go; back to the bright strange things of infancy and the quick sun-drenched glimpses of magic that old scenes brought to wide young eyes.'

This topic is closed to new replies.

Advertisement