Passing Blitz arrays to functions

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

Elendia Starman(Posted 2008) [#1]
I've actually been using Blitz3D for a while, but I have this one problem:

I have yet to figure out how to pass blitz arrays to functions...I get a "Illegal type conversion" error...


Sledge(Posted 2008) [#2]
I believe arrays are global in B3D.


Elendia Starman(Posted 2008) [#3]
I mean blitz arrays, like v[1] and v[2]...


Sledge(Posted 2008) [#4]
Sorry (tis late). Do it like-a-this.


Elendia Starman(Posted 2008) [#5]
I still get the same error

Graphics 800, 600

Type ball
	Field x#, y#
	Field v#[2]
	Field ent
	Field num
	Field link[2]
	Field linkLen[2]
	Field fixed
End Type

;pendulum balls

b2.ball = New ball ;ball #1

b2\x = 400 ;x pos
b2\y = 300 ;y pos
b2\v[1] = 0 ;initial x velocity
b2\v[2] = 0 ;initial y velocity

b2\num = 1 ;ID
b2\link[0] = 1 ;number of links
b2\link[1] = 2 ;what it is linked to
b2\linkLen[1] = 50 ;length of link
b2\fixed = 1 ;fixed or not

b.ball = New ball ;ball #2

b\x = 450
b\y = 250
b\v[1] = 0
b\v[2] = 0

b\num = 2
b\link[0] = 1
b\link[1] = 1
b\linkLen[1] = 50
b\fixed = 0

b3.ball = New ball ;ball #3

b3\x = 550
b3\y = 300
b3\v[1] = 0
b3\v[2] = 0

b3\num = 3
b3\link[0] = 1
b3\link[1] = 1
b3\linkLen[1] = 60
b3\fixed = 0

Global g#[2]
g[1] = 0
g[2] = 2

Local f#[2]
Global f2#[2]
Global v#[2]
Local f3#[2]

While Not KeyHit(1)

;	Cls

	For b.ball = Each ball
	
		If b\fixed <> 1
		
			For i = 1 To b\link[0]
		
				For b2.ball = Each ball
				
					If b2 <> b And b2\num = b\link[i]
		
						dx# = b\x - b2\x
						dy# = b\y - b2\y
						d# = Sqr#(dx*dx + dy*dy)
						
						diff# = b\linkLen[i]/d
						
						f[1] = ((dx*diff+b2\x) - b\x)*2
						f[2] = ((dy*diff+b2\y) - b\y)*2
						
;						Text 0, 20*b\num, f[1] + ":" + f[2]
						
						v[1] = b\v[1] + g[1]
						v[2] = b\v[2] + g[2]
						
						vectorProj(f3) ;projection of v onto f
						
						Color 255, 0, 0
				;		Line b\x, b\y, b\x + b\v[1]*10, b\y + b\v[2]*10
						
						Color 0, 255, 0
				;		Line b\x, b\y, b\x + f2[1]*10, b\y + f2[2]*10
						
						Color 0, 0, 255
				;		Line 400, 300, b\x, b\y
				
						b\v[1] = v[1] - f2[1]*1.5
						b\v[2] = v[2] - f2[2]*1.5
						
						Color 255, 0, 255
						Line b\x, b\y, b\x + b\v[1]*10, b\y + b\v[2]*10
						
						Color 255, 255, 255
						
;						DebugLog (f[1]*b\v[1]+f[2]*b\v[2])
;						DebugLog ""
						
					EndIf
					
				Next
			
			Next
		
		EndIf
		
	Next
		
	For b.ball = Each ball
					
		b\x = b\x + b\v[1]
		b\y = b\y + b\v[2]

		Color 255, 255, 0
		Oval b\x-5, b\y-5, 10, 10, 1
	
	Next
	
;	Flip
	
	Delay 100
	
Wend

End

;function

Function vectorProj(f[2])

	dot# = f[1]*v[1] + f[2]*v[2]
	lenF# = f[1]*f[1] + f[2]*f[2]
	scalar# = dot/lenF
	
;	DebugLog dot
;	DebugLog lenF
;	DebugLog ""
	
	f2[1] = f[1] * scalar
	f2[2] = f[2] * scalar
	
End Function


the code as it stands right now...


Sledge(Posted 2008) [#6]
You need to specify in the vectorProj function that you are passing an array of floats otherwise it expects an array of ints...

Function vectorProj(f#[2])



Elendia Starman(Posted 2008) [#7]
never mind, I fixed it

I was trying to pass a float array to an integer array...

So, for anyone else who has the problem, make sure the array being passed is the same type (float, integer, or string) as the array being used in the function.

EDIT: heh, you beat me to the answer...

New question: how would you do "f2 = vectorProj[f#, v#)"?


Elendia Starman(Posted 2008) [#8]
New question: how would you do "f2 = vectorProj[f#, v#)"?