Copying a Object and Retaining its values?

BlitzMax Forums/BlitzMax Programming/Copying a Object and Retaining its values?

Sean Doherty(Posted 2005) [#1]
Is there anyway to copy of object and retain its values? For example, if I have a laser with all it properties set and I want to create a clone of the laser with all of its properties. I'm the set I should have tow instance with all values in tact?

I wondering is there is a command so I don't have to program my own clone command.


bradford6(Posted 2005) [#2]
nope. best to program your own clone command


Perturbatio(Posted 2005) [#3]
I wonder if it would be possible to do something like this with memcopy?


N(Posted 2005) [#4]
If you want to do a shallow copy (e.g., copy all pointers and values instead cloning all associated objects), just create a new object of the same type and do this:

MemCopy( Varptr original.firstField, Varptr other.firstField, theSizeOfTheType ) ' I don't trust SizeOf due to it being rather context sensitive



Sean Doherty(Posted 2005) [#5]
What if I had simething like:

Type Laser

Field iX:int
Field fY:float
Field fZ:Double

Method Update()
  Print "Update"
End Method



bradford6(Posted 2005) [#6]
Type Laser
	Field name:String
	Field iX:Int
	Field iY:Float
	Field iZ:Double
	
	Method Update()
	  	Print "NAME:"+name
	  	Print "iX: "+iX
		 Print "iY: "+iY
		Print "iZ: "+iZ
	End Method
	
	Function copylaser:Laser(source:laser,newname:String)
		temp:laser = New laser
			temp.name = newname
			temp.iX = Source.iX
			temp.iY = source.iY
			temp.iZ = source.iZ
			
		Return temp
	
	End Function

End Type


death:laser = New laser
death.name = "Death Laser"
death.iX = 100
death.iY = 222.333
death.iZ = 666

crazylaser:laser = death.copylaser(death,"KrazyLaser")

death.Update
Print
crazylaser.Update




taxlerendiosk(Posted 2005) [#7]
Why use a function rather than a method?

	Method copy:Laser(newname:String)
		temp:laser = New laser
			temp.name = newname
			temp.iX = Self.iX
			temp.iY = Self.iY
			temp.iZ = Self.iZ
		Return temp
	End Method