Advertisement

How to use array in class

Started by November 04, 2009 01:38 AM
5 comments, last by WitchLord 15 years, 3 months ago
C++: class TestType { int i; int j[10]; }; r = m_engine->RegisterObjectProperty("TestType","int i",offsetof(TestType,i));assert( r >= 0 ); r = m_engine->RegisterObjectProperty("TestType","int[] n",offsetof(TestType,n));assert( r >= 0 ); as: TestType obj = GetTestFunc(); //return a new object obj.i = 9; obj.n[0] = 1; //Error,in void *asCArrayObject::at(asUINT index),buffer is NULL
AngelScript's arrays aren't the same as C or C++ static arrays. You need to create a instance of asIScriptArray and use that internally. You can see an example of this in the test_arrayintf test:

http://angelscript.svn.sf.net/viewvc/angelscript/trunk/sdk/tests/test_feature/source/test_arrayintf.cpp?revision=459&view=markup
Advertisement
droz is correct.

C/C++ arrays cannot be registered directly with AngelScript yet. The AngelScript array is a dynamic object, similar to std::vector, except it is reference counted.

If your classes have arrays you'll need to wrap them with functions instead.

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

Andreas: Would it be theoretically possible to use getters and setters with an additional index parameter to get the appropriate variable in a static c++ array? Of course, having the getter/setter function do the sanity checks and keeping inside boundaries. Example:

Note: this is just an idea, I have mocked up this code in 5 minutes.
// Main class declarationclass MyClass {public:	int variable[128];};// C++ class instanceMyClass mc;// Getter proxyint Proxy_getvariable(const MyClass * myclass, int index) {	if ((index) > sizeof(myclass->variable) / sizeof(myclass->variable[0]))		asGetActiveContext()->SetException("Index out of bounds!");	return myclass->variable[index];}// Setter proxyvoid Proxy_setvariable(MyClass * myclass, const int& newint, int index) {	if ((index) > sizeof(myclass->variable) / sizeof(myclass->variable[0]))		asGetActiveContext()->SetException("Index out of bounds!");	myclass->variable[index] = newint;}// Angelscript class registrationr = engine->RegisterObjectType("MyClass", 0, asOBJ_REF | asOBJ_NOHANDLE); assert( r >= 0 );r = engine->RegisterObjectMethod("MyClass", "int get_variable(int index)", asFUNCTION(Proxy_getvariable), asCALL_CDECL_OBJFIRST); assert( r >= 0 );r = engine->RegisterObjectMethod("MyClass", "void set_variable(const string &in, int index)", asFUNCTION(Proxy_setvariable), asCALL_CDECL_OBJFIRST); assert( r >= 0 );r = engine->RegisterGlobalProperty("MyClass mc", &mc); assert( r >= 0 );// Angelscript script examplevoid main() {	mc.variable[10] = 33;}

Yes, that is theoretically possible. It is actually a very good suggestion that I'll add to my to-do list for further investigation.

Thanks!

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

Being curious again, if this feature is to be implemented, what is the approximate Angelscript version that will add support for getter/setter indexes? Because this would potentially solve some major problems I am currently facing i.e. registering a C/C++ array pointed to by a level N pointer. All that's needed are two proxy functions that dereference the pointer, and then perform normal array operations. It would also allow me to access them without using any function calls, which is neat. Again, I don't mean to rush you. :)

Regards,
Biz
Advertisement
I have a lot of other changes planned for version 2.18.0 so I won't have time to look into this now, however, should someone like to send the necessary changes to me I can definitely add it. ;)

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