Passing arrays to functions..

Blitz3D Forums/Blitz3D Beginners Area/Passing arrays to functions..

ErikT(Posted 2003) [#1]
I need to pass an array to a function but can't quite figure out how to do it. The below code won't work, I get an error down by the function declaration saying it expects a ')':

[CODE]

Dim Walk(6)
For x = 0 To 5
Walk(x) = LoadImage("walk_"+x+".bmp")
Next

If something..
Render_This(Walk(6),64,64)
EndIf

Function Render_This(Image(6),X#,Y#)
; code..
[/CODE]

Any code gurus around to show me what's what? :)


Steve Hill(Posted 2003) [#2]
Arrays in Blitz are global and can't be passed to functions.

One solution is just to use Walk in the Render_This function, albeit that this not very modular, but then we are dealing with a Basic language here.

There are things called "blitz arrays" that are not documented and can be passed to functions and made members of types etc.

Here is a (very silly) example. Notice that the arrays are passed by "reference" which means that the function can modify the array that is passed in and the results are seen at the top level. The same would be true for entities etc.

Global c[10]

Type T
	Field b[10]
End Type

Function fred(x[10])
	Print x[1]
	x[1] = 23
End Function

k.T = New T

k\b[1] = 1
c[1] = 2

fred(k\b)
fred(k\b)
fred(c)

WaitKey


If you'd rather stay within the documented stuff, you are probably going to have to use a bank instead.

Hope this helps,

Steve.


ErikT(Posted 2003) [#3]
Thanks a lot. I'll take a look at it..


ErikT(Posted 2003) [#4]
Hmm, I couldn't get those 'blitz arrays' to do what I wanted. Seems the image handle returns zero when I try assigning them to images. Just to make sure I understood you correctly; this undocumented array type is declared like this: 'Global x[4]', right?


Steve Hill(Posted 2003) [#5]
This works for me (I'm using Blitz3D if that makes any difference)

Function DrawImages(img[4])
	For i = 0 To 4
		DrawImage img[i], 100+i*20, 100+i*20
	Next
End Function

Global images[4], otherImages[4]

Graphics 640, 480

images[0] = LoadImage("image1.jpg")
images[1] = LoadImage("image2.jpg")
images[2] = LoadImage("image3.jpg")
images[3] = LoadImage("image4.jpg")
images[4] = LoadImage("image5.jpg")

DrawImages(images)

WaitKey

otherImages[0] = LoadImage("image5.jpg")
otherImages[1] = LoadImage("image4.jpg")
otherImages[2] = LoadImage("image3.jpg")
otherImages[3] = LoadImage("image2.jpg")
otherImages[4] = LoadImage("image1.jpg")

DrawImages(otherImages)


Steve.


ErikT(Posted 2003) [#6]
My bad, I had accidentally changed image names so the program couldn't locate the images. Works great now, thanks again. :)