Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: copying values between objects.

  1. #1

    copying values between objects.

    How do i make an copy/clone of an object.
    E.g. i want to copy vars from one existing object to another existing object.
    This works as long as i do not free the object i copied from.
    How do i make a real copy with values instead of references? So that i can free the object i copied from and continue using the object i copied to.

    Thanks for your help in advance.
    http://3das.noeska.com - create adventure games without programming

  2. #2

    copying values between objects.

    if you mean creating a "deep" copy, then there is no generic way which would work on all objects....you have to create a constructor which takes object of the same type as one of its arguments and there you have to copy all the values manually....after that you can use the constructor to create clones....

  3. #3

    copying values between objects.

    The only way i can think of is if you added an assign procedure into the object..

    Code:
    procedure TObject.Assign(const Source: TObject);
    begin
      var1 := Source.var1;
      var2 := Source.var2;
      .. etc ..
    end;
    In the case where you have TObject2 = Class(TObject) you can make the Assign a virtual procedure and you can do..

    Code:
    procedure TObject2.Assign(const Source: TObject); override;
    begin
      inherited Assign(Source);
      If (Source Is TObject2) Then
      Begin
        newVar1 := TObject2(Source).newVar1;
        newVar2 := TObject2(Source).newVar2;
      End;
    end;
    Hope this helps
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  4. #4

    copying values between objects.

    I don't know which compiler you are using but in Delphi you have Assign method already defined. However it works only for the descendants of the TPersistent class.

  5. #5

    copying values between objects.

    Assuming you use properties to hold you important information, IIRC you should be able to use RTTI to copy the values over. In either case the correct way to do it is by implementing an *Assign* method.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  6. #6

    copying values between objects.

    Should i use Assign or AssignTo for this, i started writing AssignTo as i believe Assign is dependen on Assignto i believe, but i am not sure.

    @savage: how do i use: IIRC you should be able to use RTTI to copy the values over.

    [EDIT]
    Hmm the real problem was array of TMesh.
    It should also have an Assign method. Also for the copy i should make new TMeshes and assign the values to these.
    [/EDIT]
    http://3das.noeska.com - create adventure games without programming

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    copying values between objects.

    Hey noeska,

    Since I use objects with about every gaming project I make this is an issue that I deal with all the time. My solution is to make my own function that clones all the values.

    [pascal]procedure TGameObject.Clone(Original: TGameObject);
    begin
    X := Original.X;
    Y := Original.Y;
    // etc... for all values
    end;[/pascal]

    Much like M109uk's Assign procedure above. This allows you to make a clean looking loop for things like removing one of an array of weapon shots that hit their target for example.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8

    copying values between objects.

    Quote Originally Posted by noeska
    @savage: how do i use: IIRC you should be able to use RTTI to copy the values over.
    I don't have a specific example to hand, but I found the legendary Brian long has a solution in this zip file...
    http://www.blong.com/Conferences/Bor...RTTI/CB140.zip

    Quote Originally Posted by Brian Long
    The RTTIUnit.pas unit that accompanies this paper has a routine called CopyObject defined in it. CopyObject takes two object references - a source object and a target object. It iterates through all the published properties in the source object and attempts to set the value of the same property in the target. If the property doesn’t exist in the target then it skips onto the next one.
    I think this is quite an elegant solution.

    Which is taken from this article here - http://www.blong.com/Conferences/Bor...RTTI/CB140.htm
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  9. #9

    copying values between objects.

    and it is a lot slower than just declaring the copy function by yourself + it does handle deep copying classes only partially - from RTTI you can get class type info and out of that class reference, but that's all, you do not know how to clone the nested classes (that is using standard .Create constructor you can create it, but not assign any data do it)....

  10. #10

    copying values between objects.

    I made this little code that uses Streams. Not very elegant solution and it does not work with tree like objects. You can manually iterate every sub-object though, this style is for saving fixed length blocks of data to stream or file. On the positive side, copying objects can't get any faster than this

    For managing size of data block you need to give out first var, last var and size of the last one.
    http://mureakuha.com/koodikirjasto/1013

    Why not use records instead? Because these objects can have functions with them as well.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •