Sign in to follow this  
AlphaDog

arrays and handles question..

Recommended Posts

AlphaDog    122
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]

Share this post


Link to post
Share on other sites
WitchLord    4678
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

Share this post


Link to post
Share on other sites
AlphaDog    122
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]

Share this post


Link to post
Share on other sites
WitchLord    4678
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.

Share this post


Link to post
Share on other sites
WitchLord    4678
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this