Sign in to follow this  

Two questions about arrays

Recommended Posts

1) Is there any way, to register native C++ static arrays?
[s]2) I followed steps from this example code: [url=""]http://www.angelcode...ddon_array.html[/url] to register CScriptArray containing Block_Vector's (it's my custom class). How should I register that kind of array as a global property? I did it this way: [/s]

[code]CScriptArray* resultsV;
RegisterScriptArray(ASM->GetEngine()->m_ptr, true);

asIObjectType* t = ASM->GetEngine()->m_ptr->GetObjectTypeById(ASM->GetEngine()->m_ptr->GetTypeIdByDecl("array<Vector>"));
resultsV = new CScriptArray(1000000, t);
for( int i = 0; i < 1000000; i++ )
Block_Vector** p = static_cast<Block_Vector**>(resultsV->At(i));
*p = new Block_Vector(0.0f, 0.0f, 0.0f, 0.0f);

ASM->GetEngine()->m_ptr->RegisterGlobalProperty("array<Vector> g_resultsV", &resultsV); [/code]
When I use g_resultsV in script in loop, this statement causes crash: g_resultsV[ no ].
It looks like my CScriptArray is totally wrong and uninitialized, it's buffer is NULL.
What should I fix in my code?

I'm using the latest 2.20.3 version of Angel Script.

Ad 2) It was stupid mistake, it was here:
ASM->GetEngine()->m_ptr->RegisterGlobalProperty("array<Vector> g_resultsV", &resultsV);
I passed pointer to pointer, working version looks like this:
ASM->GetEngine()->m_ptr->RegisterGlobalProperty("array<Vector> g_resultsV", resultsV);

It still crashes :/.

Again... it was my mistake :).
Working code:
Block_Vector* p = static_cast<Block_Vector*>(resultsV->At(i);
p = new Block_Vector(0.0f, 0.0f, 0.0f, 0.0f);

But my question number 1 is still open :).

Share this post

Link to post
Share on other sites
The only way to directly register a static array, is as if it was a fixed size structure, for example a float[3] can be registered with RegisterObjectType("float3", sizeof(float[3]), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS);. Of course this only works if the array is always the same size.

If the array is a member of a class, it is probably easiest to use property accessors to expose the array itself rather than trying to register a type to represent the array.

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