I'm new to angelscript and am working through a few exercises to learn more about it. I've encountered a problem that is likely the result of a key misunderstanding about how AS functions.
The exercise is to share an object between the application and AS using a pointer passed as an argument:
- App: Instantiate object.
- App: Execute script
- Script: Instantiate object handle
- Script: Call application function with handle as argument
- App function: Set object pointer (handle) to object address. Return.
- Script: modify object (through handle). End.
- App: use object. End.
The error occurs once the script attempts to modify the object through the handle (step 6). The handle still points to null, suggesting that the address assignment did not occur correctly. What am I missing here?
My goal with this post is mostly to understand why my current solution does not work but I'm also interested to know if there are other ways to share objects without registering them as a global property.
Registering the object (Person)
[source]
//register the type
r = engine->RegisterObjectType("Person", 0, asOBJ_REF); assert(r>=0);
//register the object properties
r = engine->RegisterObjectProperty("Person", "string name", asOFFSET(Person, name)); assert(r>=0);
r = engine->RegisterObjectProperty("Person", "int age", asOFFSET(Person, age)); assert(r>=0);
//register the constructors
r = engine->RegisterObjectBehaviour("Person", asBEHAVE_FACTORY, "Person@ f()", asFUNCTION(PersonFactory), asCALL_CDECL); assert( r >= 0 );
//register the behaviors
r = engine->RegisterObjectBehaviour("Person", asBEHAVE_ADDREF, "void f()", asMETHOD(Person,AddRef), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("Person", asBEHAVE_RELEASE, "void f()", asMETHOD(Person,Release), asCALL_THISCALL); assert( r >= 0 );[/source]
Function to be called from script:
[source]
r = engine->RegisterGlobalFunction("void GetPersonInstance(Person@)", asFUNCTION(GetPersonInstance), asCALL_CDECL);
...
Person person1;
void GetPersonInstance(Person *ptr)
{
ptr = &person1;
}
[/source]
script code:
[source]
void main()
{
Person@ b;
GetPersonInstance(@b);
b.name = "Jane";
b.age = 40;
}
[/source]