Copying types

BlitzPlus Forums/BlitzPlus Beginners Area/Copying types

Hi,

I'm probably doing something dull, but can anyone shed any light on how to copy a list of types into a backup list of types.

Type player
Field x_pos
Field y_pos
End Type

Type backup_player
Field x_pos
Field y_pos
End Type

Ideally I need to copy each instance of 'player' into a new list of types 'backup_player'

eg.

For my.player To Each player
copy.backup_player = New backup_player
copy.backup_player = my.player
Next

Or something similar, thanks to anyone who can shed any light on this as

copy them to a bank (poke), then copy from bank (peek).

As long as there are no strings involved its straight forward.

Otherwise write your own copy function which copies all the fields across "manually". I do this to init a type from a template type.

Being exceedingly cheeky...

Is it possible to furnish me with some sample code as I did
try this before to no avail - probably doing something stupid.

Thanks to all who replied.

I can't give you the bank code but I have a type called Man and many functions that deal with the Man Type inc. this one:

Function ManInit(m.Man, t.Man) ;m = man to be Initialised, t = template to use
	m\ManType = t\ManType
	m\Active = 1
	m\x = t\x
	m\y = t\y
	m\XAcc = t\XAcc
	m\YAcc = t\YAcc
	m\CentreX# = t\CentreX
	m\CentreY# = t\CentreY
	m\XSpeed = t\XSpeed
	m\YSpeed = t\YSpeed
	m\Width = t\Width
	m\Height = t\Height
	m\Weight = t\Weight
	m\Health = t\Health
	m\HealthMax = t\HealthMax
	m\Shield = t\Shield
	m\ShieldMax = t\ShieldMax
	m\Power = t\Power	
	m\Direction = t\Direction
	m\Pattern = t\Pattern
	m\PatternCounter = t\PatternCounter
	m\PatternReset = t\PatternReset	
	m\ShootCounter = 0
	m\ShootMax = t\ShootMax
	m\CurrentImage% = t\TemplateImages
	m\CurrentFrame% = t\CurrentFrame
	m\Template = 0	
	
End Function


Thanks for that, will give it a whirl.

i might have misunderstood your question, if i didnt, here is the code:). Most of it not required, just put in a few checking options.

; copy and checking data
; copyright Nicholas Tindall 2005
; *******************************

Graphics 800, 600, 16, 2
SeedRnd MilliSecs()
SetBuffer BackBuffer()

;setup something to test
Type badguy
	Field posx
	Field posy
	Field speed#
	Field number
End Type

For a = 1 To 1000
	bad.badguy = New badguy
		bad\posx = Rnd (0, GraphicsWidth()) ; randomly enter some data
		bad\posy = Rnd (0, (GraphicsHeight() / 2) ) + (GraphicsHeight() / 2)
		bad\speed# = Rnd (0, 10) / 100
		bad\number = a
Next

Type newbadguy	; type the data will be transfered too
	Field posx
	Field posy
	Field speed#
	Field number
End Type

;we will need 2 bytes for some of posx and posy values, 4 bytes for the float and 2 bytes for the number * by 1000:
; (2+2+4+2)= 10 * 1000 = 10000

bankname = CreateBank(10000)

offbank = 0 ; offbank is the offset to poke each value to
For bad.badguy = Each badguy
	PokeShort bankname, offbank, bad\posx
	offbank = offbank + 2	; increment offbank by the bytes allowed for it, ie 2,2,4 then 2
	PokeShort bankname, offbank, bad\posy 
	offbank = offbank + 2
	PokeFloat bankname, offbank, bad\speed#
	offbank = offbank + 4
	PokeShort bankname, offbank, bad\number
	offbank = offbank + 2
Next

Global dirPath$ = "" ; change it to a specific directory if you wish

Repeat
	bad.badguy = First badguy
	
	If KeyHit(88) ;F12
		savingdata = WriteFile(dirPath$ + "checktype.txt")
		For bad.badguy = Each badguy
			savedata$ = Str bad\posx +"  "+ Str bad\posy +"  "+ Str bad\speed# +"  "+ Str bad\number
			WriteLine savingdata, savedata$
		Next
		CloseFile(savingdata)
	EndIf

	If KeyHit(67) ;F9 
		savingdata = WriteFile(dirPath$ + "checkbank.txt")
		offbank = 0
		For a = 1 To 1000
			x = PeekShort (bankname, offbank) ;reading a bank is almost the same as writing to one, poke To place Data in a bank - peek To Read that Data
			offbank = offbank + 2	; increment offbank by the bytes allowed for it, ie 2,2,4 then 2
			y = PeekShort (bankname, offbank)
			offbank = offbank + 2
			speed# = PeekFloat (bankname, offbank)
			offbank = offbank + 4
			number = PeekShort (bankname, offbank)
			offbank = offbank + 2
			savedata$ = Str x +"  "+ Str y +"  "+ Str speed# +"  "+ Str number
			WriteLine savingdata, savedata$
		Next
		CloseFile(savingdata)
	EndIf

	If KeyHit(66) ;F8
		savingdata = WriteFile(dirPath$ + "checknewtype.txt")
		For newbad.newbadguy = Each newbadguy
			savedata$ = Str newbad\posx +"  "+ Str newbad\posy +"  "+ Str newbad\speed# +"  "+ Str newbad\number
			WriteLine savingdata, savedata$
		Next
		CloseFile(savingdata)
	EndIf


	If KeyHit(63) ; F5
		offbank = 0 ; offbank is the offset when peeking, should be the same as when poking
		For a = 1 To 1000
			newbad.newbadguy = New newbadguy ; create a new type then read the data into the type variables
			newbad\posx = PeekShort (bankname, offbank) ;reading a bank is almost the same as writing to one, poke To place Data in a bank - peek To Read that Data
			offbank = offbank + 2	; increment offbank by the bytes allowed for it, ie 2,2,4 then 2
			newbad\posy = PeekShort (bankname, offbank)
			offbank = offbank + 2
			newbad\speed# = PeekFloat (bankname, offbank)
			offbank = offbank + 4
			newbad\number = PeekShort (bankname, offbank)
			offbank = offbank + 2
		Next
	EndIf

	If KeyHit(62) ; F4
		bad.badguy = Last badguy ;we want the Last Type item so we know how many there are - placed in testcheck
		testx = 0 : testy = 0 : testspeed = 0 : testnumber = 0 : testcheck = bad\number
		For bad.badguy = Each badguy
			If bad\number = 1
				newbad.newbadguy = First newbadguy
			Else
				newbad = After newbad
			EndIf
			If newbad\posx = bad\posx Then testx = testx + 1
			If newbad\posy = bad\posy Then testy = testy + 1
			If newbad\speed# = bad\speed# Then testspeed = testspeed + 1
			If newbad\number = bad\number Then testnumber = testnumber + 1
		Next
	EndIf

	; i sometimes save data to a file then spot check to see if its correct, can also compare the data 
	; with code - looking at what i get in a file sometimes helps me debug
	Text GraphicsWidth() / 2, 20, "Press F12 to save Type data to a file", True, False
	Text GraphicsWidth() / 2, 40, "Press F9 to save Bank data to a file for checking purposes", True, False
	Text GraphicsWidth() / 2, 60, "Press F8 to save NEW Type data to a file for checking purposes", True,False
	Text GraphicsWidth() / 2, 80, "Press F5 to copy Bank data to the new type 'newbadguy' ", True,False
	Text GraphicsWidth() / 2, 100, "Press F4 to check old and new type info", True,False
	Text GraphicsWidth() / 2, 120, "'Esc' exits", True,False

	If testx > 0 And testx < 1000
		Text GraphicsWidth() / 2, 160, "posx had Errors in the copying!"
	ElseIf testx = 1000
		Text GraphicsWidth() / 2, 160, "posx had No Errors in the copying:)"
	EndIf
	If testy > 0 And testy < 1000
		Text GraphicsWidth() / 2, 170, "posy had Errors in the copying!"
	ElseIf testy = 1000
		Text GraphicsWidth() / 2, 170, "posy had No Errors in the copying:)"
	EndIf
	If testspeed > 0 And speed < 1000
		Text GraphicsWidth() / 2, 180, "speed# had Errors in the copying!"
	ElseIf testspeed = 1000
		Text GraphicsWidth() / 2, 180, "speed# had No Errors in the copying:)"
	EndIf
	If testnumber > 0 And testnumber < 1000
		Text GraphicsWidth() / 2, 190, "number had Errors in the copying!"
	ElseIf testnumber = 1000
		Text GraphicsWidth() / 2, 190, "number had No Errors in the copying:)"
	EndIf

	If testx = 1000 And testy = 1000 And testspeed = 1000 And testnumber = 1000
		Text GraphicsWidth() / 2, 220, "ALL fields copied correctly", True, False
	EndIf

	Flip
Until KeyHit(1)

Delete Each newbadguy
Delete Each badguy
FreeBank bankname		; free bank when finished with it, so it frees up the memory
EndGraphics


Thanks for all your help, I'll give both ideas a try over the weekend.