Skip to main content
gamedev.net
🔒 Locked

Registering a method which returns an array

Started by Cheetah3D 5 replies 1.7k views
Original Post
Cheetah3D
Cheetah3D
Hi, is it possible to register a function which returns an array? How can I properly setup asIScriptArray to return it in an generic function call? For example I want to extend my string class with a method which returns an array of strings with all the path components of an file system path like /usr/include/zlib.h -> {usr, include, zlib.h} Bye, Martin
Deyja
Deyja
IIRC, Angelscript arrays aren't binary compatible with C-style arrays. You'll have to register a class to handle it. std::vector will do the job fine.
Cheetah3D
Cheetah3D
Hi Deyja,
I also thought about registering my own array class. But since it is possible to send AS arrays to C functions via the asIScriptArray class wouldn't it be cool if I could also return asIScriptArray objects from my C functions.

So my users won't have to deal with two different types of arrays. The AS arrrays and my own registered Array class.

I think it wouldn't be to difficult to add that feature to AS. I think just the asIScriptArray class has to be extended slightly with a constructor and some "Set()" functions.

Would that be possible WitchLord.

Bye,
Martin
WitchLord
WitchLord
You should be able to create the asIScriptArray the following way:

int arrayTypeId = engine->GetTypeIdByDecl(0, "string[]");asIScriptArray *array = (asIScriptArray*)engine->CreateScriptObject(arrayTypeId);array->Resize(2);((asCScriptString*)array->GetElementPointer(0))->buffer = "String 1";((asCScriptString*)array->GetElementPointer(1))->buffer = "String 2";


The arrayTypeId can of course be cached.

I haven't tested the above code, so let me know if you're having problem getting it to work.

Regards,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Cheetah3D
Cheetah3D
Hi,
yes it works. At least for a simple conversion of my vector class to a float array. Thanks for the tip. :)

Another question. Is there a difference if I use "float[] &array()" or "float[]@ array()". Both work.

r = engine->RegisterObjectMethod("Vec4D", "float[] &array()",asFUNCTION(csVec4DArray),asCALL_GENERIC); assert(r >= 0);r = engine->RegisterObjectMethod("Vec4D", "float[]@ array()",asFUNCTION(csVec4DArray),asCALL_GENERIC); assert(r >= 0);



Bye,
Martin
WitchLord
WitchLord
The difference is the memory management. :)

By returning a float[]& you're telling AngelScript that you're returning a reference to an object managed by the application, thus AngelScript shouldn't release the object.

By returning a float[]@ you're telling AngelScript that it is receiving a pointer with the reference accounted for. AngelScript will call Release() on this pointer when it is done with it.

If you're using CreateScriptObject() to create a new array for each call then you should use float[]@, since you are not keeping the reference for later use.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Cheetah3D
Cheetah3D
Hi,
thanks for the help. It works like a charm. Now I've also tested it with my string class and it worked to.

But for the some reasons the strings in my array didn't get released. But that could be my fault too. I have to check that first.

Bye,
Martin

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.