Game Development Community

Question about CopyTo

by Scott Goodwin · in Torque X 2D · 02/15/2008 (11:00 pm) · 2 replies

I have a question about the correct way to implement CopyTo.

TorqueObjects are supposed to implement
public override void CopyTo(TorqueObject obj)
to copy all public properties not marked with TorqueCloneIgnore attribute.

The typical way of doing this is
public override void CopyTo(TorqueObject obj)
        {
            base.CopyTo(obj);
   
            TheClass obj2 = (TheClass)obj;
            obj2.APublicProperty = APublicProperty;
        }

What if the property is a reference type? Many classes in Torque X do this:
public override void CopyTo(TorqueObject obj)
        {
            base.CopyTo(obj);
   
            TheClass obj2 = (TheClass)obj;
            obj2.APublic_ReferenceType_Property = APublic_ReferenceType_Property;
        }

Is that right? Or should the referenced object by copied/cloned?

#1
02/16/2008 (7:57 pm)
Exactly. References are copied just like properties, so the new cloned instance has its own reference to the original object. Your example looks perfect.

John K.
#2
02/17/2008 (9:58 am)
My worry is about objects like GUIControl. I'm not exactly sure under what circumstances CopyTo gets called. But if it does, GUIControl.CopyTo copies the Style (reference) property. In the copied object if we were to do something like copiedObject.Style.IsOpaque = true; then the original (and all copies) sudden become opaque. Etc. I'm not sure this is what is wanted/expected.