Jump to content
  • Advertisement
Sign in to follow this  
LDenninger

Array confusion :)

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hithere, I'm trying to remove my own old ASArray-template-code; I used to have a function that was registered as "void DoSomething(const VertexArray &in)". Now I'd like to use the AS built-in array stuff for this, but I don't know how to register that same function. If I have registered a custom object-type, how do I register a function that will take a asCArrayObject of that type ?

Share this post


Link to post
Share on other sites
Advertisement
You'd register the function the same way. If the array type hasn't been overloaded, AngelScript will assume that you'll use asIScriptArray interface.


void DoSomething(asIScriptArray *array)
{
if( !array ) return;

int cnt = array->GetElementCount();
for( int n = 0; n < cnt; n++ )
{
MyObj *p = (MyObj*)array->GetElementPointer(n);
... do something with the object
}

// If we receive the array as object handle, it must be released
array->Release();
}


I recommend registering the function to take the array by handle, since it avoids unnecessary copies.


int r = engine->RegisterGlobalFunction("void DoSomething(MyObj[]@)", asFUNCTION(DoSomething), asCALL_CDECL); assert( r >= 0 );


Regards,
Andreas

Share this post


Link to post
Share on other sites
Okay, I must be doing something very stupid 'cause I can't get it to work :)

Let me show you what I do :

#1:
I register my vertex class.
result = outEngine->RegisterObjectType("Vertex", sizeof(Vertex), asOBJ_CLASS | asOBJ_CLASS_CONSTRUCTOR | asOBJ_CLASS_ASSIGNMENT); assert(result>=0);


#2
I have a cpp-function that needs an array of Vertices.
Declared/implemented in cpp as :
asDraw2DTriangleFan(asCArrayObject * inVertices)
{
if (!inVertices)
return;

int num_vertices = inVertices->GetElementCount();

if (num_vertices >= 3)
{
Vertex * vertex_list = (Vertex * )inVertices->GetElementPointer(0);

// Draw
VERIFY_D3D_CALL(mDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, num_vertices - 2, vertex_list, sizeof(Vertex)));
}

// We received the array as object handle, it must be released
inVertices->Release();
}


#3
I register that function to AS as:
"void Draw2DTriangleFan(Vertex[]@)"

#4
The script :

Vertex[] vertices;
vertices.resize(128)
// fill in vertices here, blah
...
Draw2DTriangleFan(vertices);



#4
The problem :
It just doesnt work :)
Even more strange, the first time the script is compiled it compiles fine,
but when I reload the script sometimes it comes up with a message saying
"Draw2DTriangle(Vertex[]&)" is not defined.

Is the call in the script ("Draw2DTriangleFan(vertices);") correct ?
(if not - why wasn't there a compile-error the first time when I compiled it ?)



Please help me find out what I'm doing wrong :D

Share this post


Link to post
Share on other sites
SORRY!
I found the problem :)


I was assuming elements of the asCArrayObject would be linearly mapped in memory,
but they aren't :)

I need to copy the vertices one-by-one to a new buffer, and then it works.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!