Advertisement

Copy Constructor of asOBJ_VALUE not called

Started by May 09, 2012 07:20 PM
3 comments, last by WitchLord 12 years, 6 months ago
Hi,

Another day, another problem :(

i have this object (not a POD)

r = engine->RegisterObjectType("JsVariable", sizeof(JsVariable), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK); assert( r >= 0 ); // CDAK stands for class constructor assignment and copy constructor as i understand

// destructor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(JsVariable::JsVariableDestructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// default constructor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(JsVariable::JsVariableDefaultConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// copy constructor. not called :(
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f(const JsVariable &in)", asFUNCTION(JsVariable::JsVariableCopyConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// another constuctor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(JsVariable::JsVariableFloatConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// another constuctor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f(const string &in)", asFUNCTION(JsVariable::JsVariableStringConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// another constuctor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(JsVariable::JsVariableBoolConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// == operator
r = engine->RegisterObjectMethod("JsVariable", "bool opEquals(const JsVariable &in) const", asFUNCTION(JsVariable::JsVariableEquals), asCALL_CDECL_OBJFIRST); assert( r >= 0 );



I create object as a part of CScriptArray

asIObjectType* t = ScriptManager::getSingletonPtr()->engine->GetObjectTypeById(ScriptManager::getSingletonPtr()->engine->GetTypeIdByDecl("array<JsVariable>"));

CScriptArray* arr = new CScriptArray(1, t); // default constructor successfully called.
JsVariable js = JsVariable((string)"test !");
arr->SetValue(0, &js);


when this line hits on scriptarray.cpp

objType->GetEngine()->CopyScriptObject(ptr, value, subTypeId);

i expect copy constructor to be called.
instead none of the constructors called. JsVariable is passed as it's created by CScriptArray* arr = new CScriptArray(1, t); . (default constructor)

copy constructor is this.

static void JsVariableCopyConstructor(const JsVariable &other, JsVariable *self)
{
new(self) JsVariable(other);
}


script function called is this (not really much related to question, but i may be wrong)

void UIOnCallback(string funcname, array<JsVariable> @arr) // arr is passed here correct. JsVariables come empty (default constructor), array length is correct
{
//do stuff
}


i kinda post a lot of code. I just want to clear the issue as it is.
i feel like i miss something very simple. but it's been 4 hours since i started staring at it.

why does not the copy constructor called?

thank you for your time
ok solved it.

i did not expect that i need = operator to call copy constructor. (in script there is never a need for = operator)

added this


r = engine->RegisterObjectMethod("JsVariable", "JsVariable opAssign(const JsVariable &in)", asFUNCTION(JsVariable::JsVariableCopyConstructor), asCALL_CDECL_OBJLAST) ; assert( r >= 0 );
Advertisement
The CopyScriptObject() method doesn't create a new instance. It expects two existing objects, and will do a value assign.

The CreateScriptObjectCopy() method is the one that creates a new instance of the object. Though even that doesn't use the copy constructor yet, it creates a new instance using the default constructor then calls the opAssign method to copy the value.

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

I don't think this is the first time someone had this confusion with the CopyScriptObject() function. Maybe deprecate it and add a function called something like AssignScriptObject()? I think that would be a little clearer that it wraps the assignment behavior.
That's true. I might just do this.

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