best way to copy a type to a bank?

Blitz3D Forums/Blitz3D Programming/best way to copy a type to a bank?

Hi, is there a speedy way to copy a tpye to a created bank?

try this wrote it a few days ago for someone.

most of the code is for checks etc.

I've just finishing restoring a backup, so hope its ok:)

; 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, nicoust, but it's a typical way in blitz to copy a type to a bank. Is there a way just like c++ does:
memcpy(&bank,&type,sizeof(type))?

nope nothing so straightforward.

Although its not as bad as my code looks, is only a few lines:)

Guan: there isn't an easy way to copy type data to a bank. However, there are ways to store type references to banks or plain integers. The Object and Handle keywords are probably the least properly explained tools in the Blitz3D language. Basically you can use Handle() to return an integer for your type instance, i.e. hand% = handle(mybaddie.typeObj). You can then use the object keyword to regain access to your type instance, i.e. yourbaddie.typeObj = object.typeObj(hand%). Since each type instance has its own unique number returned from Handle(), you'll always get the same object back.

Now, there's nothing stopping you from storing each of these integers in a bank, and so then you'll have your type instances listed in your bank. But don't go deleting the actual type instance, as then you'll loose all the data anyway. Granted this may not be anythig you're looking for, but I thought it was worth a mention anyway.

If you want to store the actual contents of your type instance within a bank, you'll have to do it manually, variable by variable, and write a function to do the reverse operation, and build a type instance. I think one of the reasons Blitz doesn't do deep copies is that it's hard to say how deep you can go, when one type may reference countless others of major importance.