Jump to content
  • Advertisement
Sign in to follow this  
hanchong

How to use array in class

This topic is 3111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 declaration
class MyClass {
public:
int variable[128];
};

// C++ class instance
MyClass mc;



// Getter proxy
int 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 proxy
void 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 registration
r = 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 example
void main() {
mc.variable[10] = 33;
}

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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. ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!