Clone object

Started by
3 comments, last by WitchLord 9 years, 10 months ago

Hello.

It is possible to clone whole object if we don't know its structure in place where we want do it?

Example:

class c1{}

class c2{}

c1@ handle;

void foo()

{

@handle = c2();

}

void foo2()

{

c1 a1 = handle; // here we clone only c1 part of whole object

c1@ a2 = "clone(handle); // but I want clone whole object and get c2 class, not c1

c2@ a3 = cast<c2>(a2);

}

Advertisement

How about implementing a clone method in the script class?

class c2 : c1
{
  c2 @clone() { c2 c(this); return c; }
}

Else you could implement a clone() function in C++, using the CScriptHandle add-on to receive and return variable handles:

// Registered with AngelScript using the signature 'ref @clone(ref @)'
CScriptHandle clone(CScriptHandle hndl)
{
   // Get the handle and type of the received object
   void *obj1 = 0;
   hndl.Cast(&obj1, hndl.GetTypeId());
   asIObjectType *type = hndl.GetType();
 
   // Create a copy of the object
   asIScriptEngine *engine = type->GetEngine();
   void *obj2 = engine->CreateScriptObjectCopy(obj1, type);
 
   // Initialize the CScriptHandle that will be returned
   CScriptHandle c();
   c.Set(obj2, type);
 
   // Make sure we release the handles we will not keep. Cast and Set both increment the refCount
   engine->ReleaseScriptObject(obj1, type);
   engine->ReleaseScriptObject(obj2, type);
 
   return c;
}

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

First solution is not good, because will be many classes to add clone function to all, and user may forgot about it.

Second solution not works if handle is on base type.

I want it use for gui system, where i want clone gui classes to create multiinstance screens.

class GUIObject // Base class

{

GUIObject@ Clone() { return clone(this); } // I want clone whole object in base class

void Draw() { OnDraw(); }

void OnDraw() {}

void Move() { OnMove(); }

void OnMove() {}

}

class UserGUIObject : GUIObject // User can extend objects to store own data and behaviours, override base methods (used for callbacks)

{

UserGUIObject@ Clone() { UserGUIObject clone(this); return this; } // I want avoid this solution, bacause user can forgot about this implementation

void OnMove()

{

}

}

// GUI system to handle interaction

RegisterGUI(UserGUIWindows());

GUIObject@[] WindowObjPrototype;

GUIObject@[] WindowObjActive;

void RegisterGUI(GUIObject& obj)

{

WindowObjPrototype.insertLast(obj);

}

void InitGUI()

{

WindowObjActive.clear();

for(uint i = 0; i < WindowObjPrototype.length(); i++)

WindowObjActive.insertLast(WindowObjPrototype.Clone());

}

DrawGUI(GUIObject& obj)

{

for(uint i = 0; i < WindowObjActive.length(); i++)

WindowObjActive.Draw();

}

MoveGUI(GUIObject& obj)

{

for(uint i = 0; i < WindowObjActive.length(); i++)

WindowObjActive.Move();

}

I think i found solution, without CScriptHandle (i do not use this addon).

This works for me fine (tested).

Please check it, maybe i miss some ref count or do it not optimally:


// Bind: void CloneObject(?&in, ?&out)
// Usage: CloneObject(@from, @to);
void CloneObject( void* in, int in_type_id, void* out, int out_type_id )
{
	if( !( in_type_id & asTYPEID_OBJHANDLE ) )
		FAIL( "Invalid in arg, not an handle." );
	if( !( in_type_id & asTYPEID_SCRIPTOBJECT ) )
		FAIL( "Invalid in arg, not an script object." );
	if( !*(void**) in )
		FAIL( "Invalid in arg, handle must be non null." );
	if( !( out_type_id & asTYPEID_OBJHANDLE ) )
		FAIL( "Invalid out arg, not an handle." );
	if( *(void**) out )
		FAIL( "Invalid out arg, handle must be null." );

	in = *(void**) in;
	asIScriptObject* in_obj = (asIScriptObject*) in;
	in_type_id = in_obj->GetTypeId();
	asIScriptEngine* engine = in_obj->GetEngine();

	if( !engine->IsHandleCompatibleWithObject( in, in_type_id, out_type_id ) )
		FAIL( "Handle not compatible with object." );

	*(void**) out = engine->CreateScriptObjectCopy( in, engine->GetObjectTypeById( in_type_id ) );
}

Yes, This should work just fine.

There are no problems with the reference counts :)

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