Fast way to copy a class?

BlitzMax Forums/BlitzMax Programming/Fast way to copy a class?

Say I have two classes of the same type, level1:tlevel and level2:tlevel. I wish to have level2 completely identical to level1, for instance level2.x and level1.x should be the same. First thought that comes to mind would be

level2 = level1


But that doesn't work ;) How can I make a fast copy of all the contents to level2 from level1? I'd hate to have to write a manual function that copies it all over, e.g. :

(pseudocode)
copylvls(level1,level2)
function copylvls(from:tlevel,to:tlevel)
    to.x = from.x ; to.y = from.y
    to.data = from.data
    to.speed = from.speed
endfunction


Surely I'm missing something very basic here, just can't figure out what :P

You'll have to write a copy routine. It would probably be best to write the routine as a Method in the type itself.
Type TLevel
   Field x:int
   Field data:int
   Field speed:Int

   Method CopyLevel(Level:TLevel)
      X = Level.X
      data = Level.data
      speed = Level.speed
   End Method
End Type

Local Level1:TLevel = new TLevel
....   Create all of level1's data
Local Level2:TLevel = new TLevel
Level2.CopyLevel(Level1) 'Copy level 1 data into Level 2


Supposedly, stuff like this has been made a little simpler with reflection, but I haven't messed with reflection any to give you any advice on it. Supposedly, you would "tag" the fields with a keyword, then in your copy routine, you would look for all the fields with that tag and copy it over. So later when you add more fields, you would not need to modify the copy routine.

Thats the way I do it. Although I use a Clone:myobject() method.
That way nested objects can be cloned just by calling the Root Class.

re Reflection.

Some interesting stuff on Object cloning HERE.

:o)

Thanks guys! Much appreciated :)