Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Clone object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 cvet   Members   -  Reputation: 175

Like
0Likes
Like

Posted 05 June 2014 - 02:28 PM

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);

}



Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3294

Like
0Likes
Like

Posted 05 June 2014 - 05:23 PM

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

#3 cvet   Members   -  Reputation: 175

Like
0Likes
Like

Posted 06 June 2014 - 01:44 AM

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[i].Clone());

}

DrawGUI(GUIObject& obj)

{

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

    WindowObjActive[i].Draw();

}

MoveGUI(GUIObject& obj)

{

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

    WindowObjActive[i].Move();

}



#4 cvet   Members   -  Reputation: 175

Like
0Likes
Like

Posted 06 June 2014 - 04:08 AM

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 ) );
}

Edited by cvet, 06 June 2014 - 04:48 AM.


#5 Andreas Jonsson   Moderators   -  Reputation: 3294

Like
0Likes
Like

Posted 06 June 2014 - 08:28 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS