🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Registering a method which returns an array
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
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.
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
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
You should be able to create the asIScriptArray the following way:
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
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
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.
Bye,
Martin
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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement