functions and arrays

Blitz3D Forums/Blitz3D Beginners Area/functions and arrays

i need this:
---------------
; Function to call
Function test( arr_test$[100] )
For n = 0 To 99
Print arr_test$[n]
Next
End Function

; Main
Type kk
Field arr$[100]
End Type
k.kk = New kk
test( k\arr$ )
WaitKey
---------------
but the blitz3d compiler show me error, can you help me?

thx

iirc you can't do that with arrays ... (tho I could be wrong :) I never use arrays this way anyway)

Try banks! If your strings all have a fixed length, a bank is easy to use.

Please explain the bank idea. I've never used them.

*making a user-friendly banktext system example now*

You cannot pass an array to a function, but you can pass a type handle:
; Function to call
Function test( a.kk )
For n = 0 To 99
Print a\arr$[n]
Next
End Function

; Main
Type kk
Field arr$[99]
End Type
k.kk = New kk

;To get a visible result
For n=0 To 99
	k\arr[n]="Field "+n
Next

test( k )
WaitKey


here ya go..

I used 'notify' here.. 'print' caused some crashes here.. I guess you know what to do if you don't have B+ .. it's all fairly obvious.

bnk=CreateTextBank(10,40) ; 10:elements, 40:stringwidth

For t=0 To 9
	SetTextbankText(bnk,t,"BlitzMax - we're waiting "+t+" months")
Next


; lousy test function
test(bnk)

FreeBank bnk ; <-- rather don't forget this! always clean-up your mess :)

End

Function test(blah)

	For t=0 To TextbankElements(blah)-1
		Notify TextbankText(blah,t)
	Next
	
	Notify TextbankElements(blah)
	Notify TextbankWidth(blah)
	
End Function


; ---------------------------------------------------------------------
;
; Textbank - by CS^TBL
;
; bank=CreateTextBank(elements,width)
; SetTextbankText(bank,element,txt$)
; a$=TextbankText(bank,element(,cut))
; width=TextbankWidth(bank)
; elements=TextbankElements(bank)
;
; bank: (b=byte, s=short)
;
; [b:B][b:T][b:X][b:T][s:ELEMENTS][s:WIDTH][text .....][text .....][ ... ][ ... ]
;
; ---------------------------------------------------------------------

Function TextbankElements(bank)
	If Not bank End
	If BankSize(bank)<8 End
	For t=0 To 3
		a$=a$+Chr$(PeekByte(bank,t))
	Next
	If a$<>"BTXT" End
	
	Return PeekShort(bank,4)
End Function

Function TextbankWidth(bank)
	If Not bank End
	If BankSize(bank)<8 End
	For t=0 To 3
		a$=a$+Chr$(PeekByte(bank,t))
	Next
	If a$<>"BTXT" End
	
	Return PeekShort(bank,6)
End Function

Function TextbankText$(bank,element,cut=True)
	If Not bank End
	If BankSize(bank)<8 End
	For t=0 To 3
		a$=a$+Chr$(PeekByte(bank,t))
	Next
	If a$<>"BTXT" End
	
	elements=PeekShort(bank,4)
	width=PeekShort(bank,6)

	If element>elements End

	a$=""
	For t=0 To width-1
		a$=a$+Chr$(PeekByte(bank,8+element*width+t))
	Next

	If cut
		a$=Trim$(a$)
	EndIf
	
	Return a$

End Function

Function SetTextbankText(bank,element,s$)
	If Not bank End
	If BankSize(bank)<8 End
	For t=0 To 3
		a$=a$+Chr$(PeekByte(bank,t))
	Next
	If a$<>"BTXT" End
	
	elements=PeekShort(bank,4)
	width=PeekShort(bank,6)
	
	If element>=elements End
	If element<0 End

	l=Len(s$)
	If Len(s$)>width End
	
	For t=0 To Len(s$)-1
		PokeByte bank,8+(element*width)+t,Asc(Mid$(s$,t+1,1))
	Next
	
End Function

Function CreateTextbank(elements,width)

	; checks
	If elements<=0
		error=True
	EndIf
	If width<=0
		error=True
	EndIf
	If error End
	
	bank=CreateBank(8+elements*width)

	; header
	PokeByte bank,0,66 ; B
	PokeByte bank,1,84 ; T
	PokeByte bank,2,88 ; X
	PokeByte bank,3,84 ; T
	
	PokeShort bank,4,elements ; 4
	PokeShort bank,6,width    ; 6
	; texts                   ; 8 ...
	
	Return bank	
End Function


YOU CAN PASS AN ARRAY TO A FUNCTION

All you have to do is remove the dollar sign from the print statement and it works
Why? god knows

the array is passed by refrence, so you can also shange the contents
the arrays have to be the same size

this works
; Function to call 
Function test( arr_test$[100] ) 
For n = 0 To 99 
Print arr_test[n] 
Next 
End Function 

; Main 
Type kk 
Field arr$[100] 
End Type 
k.kk = New kk 
k\arr[89]="hello"
k\arr[95]="wazzaa"
k\arr[99]="goodbye"
test( k\arr ) 
WaitKey 


Hmmm, wouldn't have guessed, now I just need to find a use for it.