Array confusion :)

Started by
3 comments, last by WitchLord 18 years, 4 months ago
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 ?
_-=[ "If there's anything more important than my ego around, I want it caught and shot now." ]=--=[ BeatHarness ]=-=[ Guerrilla Games ]=-=[ KillZone ]=-
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

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

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
_-=[ "If there's anything more important than my ego around, I want it caught and shot now." ]=--=[ BeatHarness ]=-=[ Guerrilla Games ]=-=[ KillZone ]=-
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.
_-=[ "If there's anything more important than my ego around, I want it caught and shot now." ]=--=[ BeatHarness ]=-=[ Guerrilla Games ]=-=[ KillZone ]=-
Yes, only primitive data types are stored linearly in one large memory block.

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

This topic is closed to new replies.

Advertisement