Hi,
I'm a Blitz newbie. I'm trying to pass an array to a function in Blitz3D by reference how can this done?
Thanks,
you mean like this?
Graphics 640,480,0,2
SetBuffer BackBuffer()
Dim array(10)
array(0)=123456789
test=GetValue(array(0))
While Not KeyHit(1)
Cls
Text 0,0,"array(0)="+array(0)+" test="+test
Flip
Wend
Function GetValue(value)
Return value
End Function
Not quite ... acutally I would like the whole array passed to the function (or better yet the reference or address). Currently what you are doing is only passing the first element which is array(0).
References / adresses: not possible. Blitz3D has no references. Only thing to interface with outside that works similar are banks
Arrays: You can use BlitzArrays to achieve that. BlitzArrays have a static size and are declared through
someArr[10] instead of dim and ()
then you can pass them by
Function SomeFunc(arr[])
...
end function
If you want or need more, you will have to use a different language (BlitzMax is capable of that as well, but not blitz3d or blitzplus)
arrays are global, you just need to dim them outside a function, then refer to them inside a function like this.
Graphics 640,480,0,2
SetBuffer BackBuffer()
Dim array(10)
array(0)=123456789
test=GetValue()
While Not KeyHit(1)
Cls
Text 0,0,"array(0)="+array(0)+" test="+test
Flip
Wend
Function GetValue()
Return array(0)
End Function
To use banks, try this:
;length = 1024, a float is 4 bytes
;(a integer is 4 bytes, too)
bank = CreateBank(1024 * 4)
For i = 0 To 1023
v# = i * 0.1
PokeFloat bank, i * 4, v#
Next
Print "Before:"
For i = 1 To 5
Print PeekFloat(bank, i * 4)
Next
BankFunction(bank)
Print "After:"
For i = 1 To 5
Print PeekFloat(bank, i * 4)
Next
WaitKey()
End
Function BankFunction(ibank)
For i = 0 To (BankSize(ibank) / 4) - 1
v# = PeekFloat(ibank, i * 4)
v# = v# * 10
PokeFloat ibank, i * 4, v#
Next
End Function
I never tried it but I'm pretty sure the DIM array structure is simple, at least for int and float. I also think the array handle may be a pointer to a structure that describes the array. If this is once hacked, you could tell the function what array you want some way.
I didn't tried it yet though there have been situations when I needed it. However, it wouldn't be a proper solution.
If someone knows how I can obtain an array handle that would work nicely.
Anyway, for now ... I think I'll go with the bank implementation because the functions that I am calling are from a "included" BB program.
Thanks
It shouldn't matter where the funcitions are...global means anything can get to it, even include files.