Hello everyone,
How can I pass a handle to another object in AS using registered application classes?
I spent 5 days trying to make that happen without any luck. I tried different methodology: the famous try-and-error, reading the manual and trying to understand concepts to do it, reviewing the examples, search the internet and this forum for a similar problem, and using try-and-error again.
After that I give up and decided that I must ask in this forum.
for a clear picture of what I am trying to archive, this is a simplified example:
class definition:
class A {
public
A(const std::string &name, int size) {
refCount = 1;
anotherObject = new AnotherClass(name, size, true);
}
...
...
void addReference() {
++refCount;
}
void releaseReference() {
if(--refCount == 0)
delete this;
}
AnotherClass *anotherObject;
private:
int refCount;
}
A *A_Factory(const std::string &name, int size) {
return new A(name,size);
}
class B {
public
B(A *aObject, int size) {
refCount = 1;
yetAnotherObject = new YetAnotherClass(aObject->anotherObject, size, 10);
}
...
...
void addReference() {
++refCount;
}
void releaseReference() {
if(--refCount == 0)
delete this;
}
private:
int refCount;
YetAnotherClass *yetAnotherObject;
}
B *B_Factory(A *aObject, int size) {
return new B(aObject, size);
}
Registration of the classes
r = engine->RegisterObjectType ("A", 0, asOBJ_REF); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("A", asBEHAVE_FACTORY, "A@ f(const string &in, int)", asFUNCTION(A_Factory), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("A", asBEHAVE_ADDREF, "void f()", asMETHOD(A,addReference), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("A", asBEHAVE_RELEASE, "void f()", asMETHOD(A,releaseReference), asCALL_THISCALL); assert( r >= 0 );
// registeration of other methods for Class A
r = engine->RegisterObjectType ("B", 0, asOBJ_REF); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("B", asBEHAVE_FACTORY, "B@ f(A &in, int)", asFUNCTION(B_Factory), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("B", asBEHAVE_ADDREF, "void f()", asMETHOD(A,addReference), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("B", asBEHAVE_RELEASE, "void f()", asMETHOD(A,releaseReference), asCALL_THISCALL); assert( r >= 0 );
// registeration of other methods for Class B
AngleScript example - an example of the wanted usage of the classes
int main() {
A @a = A("apple",5);
B @b = B(a,50);
b.callSomeMemberMethod();
}
Of course this example is one logical combination of the several try-and-error combinations.
Could someone please help on this. Thank you.
Best Regards,
Edit: change BBCode to use C syntax highlighting