Functions and returns
Blitz3D Forums/Blitz3D Programming/Functions and returns
I've been puzzeling with functions and what kind of data you can return, the help topic in Blitz 3D aren't really helping me out here...
I want to return an Array, which i have not declared before hand.. to minimize declared and shared variables...
something like this...
------------------------------------
DoSomeTransform(x,y,z)
function DoSomeTransform(x1,y1,z1)
dim tempArray(3)
tempArray(1) = x1+2
tempArray(2) = y1+4
tempArray(3) = z1+5
return tempArray
end
-------------------------------------
Im new to Blitz but have tried a lot of good ideas nothing works out.. :)....'
what can you return other than float, string, integer values (and mesh, tex ect..)
Try this:
DoSomeTransform(x,y,z)
function DoSomeTransform(x1,y1,z1)
dim tempArray(3)
tempArray(1) = x1+2
tempArray(2) = y1+4
tempArray(3) = z1+5
return tempArray()
end
not sure, because I cannot test this at the moment, but that might be helpful
Hi Folks,
You have to define the type of value you're returning.
End Function
might help ;0)
Also, don't you have to use dim temparray[3] as opposed to (3)?
I'm also in work so I can't help much on this any further.
Later,
Jes
Arrays have to be dimensioned outside of all function calls initially but may be redimensioned within a function. I don't think you can return an array from within a function although you can return a type.
You don't need to return an array, they're global.
yes i found that it's impossible to return an array, but a shame it is. cause now i have to do..
----------------
dim doSomethingArray(3)
doSomething(4,4,4) ; a function
x = doSomethingArray(1) ; the calc values
----------------
I normaly code i director, where we have a Vector variable. means we can do this...
var = vector(43,3,3)
print var.x
------------------------------------
------------------------------------
by the way why can't i place and array with the same size into another array..
array1 = array2.. ??? gave an error..?
No methods or objects in Blitz.
by the way why can't i place and array with the same size into another array..
array1 = array2.. ??? gave an error..?
Ehh?
Got a better example?
Ahh sorry, have to explane a bit better what i want...
this:
--------------
dim Array1(3)
array1 = doSomeTransform(x,y,z)
--------------
so we in one simple step calc our function and return the values (a vector in this case) into a new array....
surely what your doing can be resolved by using a type? (or an array of types). Or am I just misunderstanding?
again,
dim Array1(3)
Array1(x) = doSomeTransform(x,y,z)
- I tend not to use Arrays, or Functions - more a Types/Gosub man meself haha.
Perturbatio's right Im sure. Have you looked into using custom types?
okay no methods or objects in Blitz.
shame thou, but since the blitz forum is so good and fast we can live that :)... thanks guy's...
I still can figure out how to copy an array into another array like....
Dim normVec#(3)
Dim temp#(3)
temp# = normVec#
or
temp = normVec
or
temp() = normVec()
non worked..... i feel i bit stubied, can even copy and array. but the help file aren't giving me much help :)....
guess it's new way's of thinking eihh...
is the only right way to use types instead of an array ??.
and can you then copy/replace one type into another type.. ?
thanks.
ahh got an idea,, no objects right... but we do have some sort of object.... MESH..... would it be insane to use an Mesh's vertex point to have an VECTOR point variable... i mean an vertex have 3 variables x,y,z. just like an vector..
perhaps a pivot point would do....
do anyone follow what i mean. :)
Hi! Try this:
Dim array(3)
DoSomeTransform(8,6,5)
Function DoSomeTransform(x1, y1, z1)
array(1) = x1 + 2
array(2) = y1 + 4
array(3) = z1 + 5
End Function
Now 'array' have the modified values. As Rob said, arrays are globals.
Type Vector
Field x
Field y
Field z
End Type
You can't copy a type directly since it simply reassigns the address of the types, and doesn't copy the data.
but you could create a function to copy your type
i.e.
Function CopyVector.Vector(Source.Vector)
Local temp.Vector = New Vector
temp\x = Source\x
temp\y = Source\y
temp\z = Source\z
Return temp
End Function
Type Vector
Field x
Field y
Field z
End Type
a.Vector = New Vector
a\x = 5
a\y = -4
a\z = 10
b.Vector = CopyVector(a)
Print a\x
Print b\x
b\x = 6
Print b\x
Print a\x
WaitKey
End
Function CopyVector.Vector(Source.Vector)
Local temp.Vector = New Vector
temp\x = Source\x
temp\y = Source\y
temp\z = Source\z
Return temp
End Function
Yes i get it.. :) im so happy, desided to make a Type inside another type that holds the vector, in this way we have a good sense and order of our data. :) just like an object.....
1. how do i post something in "code".. the green trick you guy's are doing, and how to do the comment part,,, "white blended part"...
2. since it's my first attempt to code in blitz, could some one be so kind to tell me if i got it all right with optimized speed and structur und so.......
--------------------------------------------------------
Type vector
Field x#
Field y#
Field z#
End Type
Type objData
Type objData
Field obj
Field id
Field velocity.vector ; = New vector
Field pos.vector ; = New vector
Field speed
End Type
vector.vector = New vector
;***************************************
;********* Varialbe declaring **********
;***************************************
Const AmountOfBoids = 20
Dim boid.objData(AmountOfBoids)
Dim normVec#(3)
For...
For i = 1 To AmountOfBoids
boid(i) = New objData
boid(i)\obj = CreateCube()
boid(i)\id = i
; creating Vectors, we first have to create some TYPEs
boid(i)\velocity = norm(1,Rnd#(-1,0),Rnd#(-1,0))
boid(i)\pos = New vector
boid(i)\pos\x = Rnd(-10,10)
boid(i)\pos\y = Rnd(-10.10)
boid(i)\pos\z = Rnd(-10,10)
PositionEntity boid(i)\obj, boid(i)\pos\x, boid(i)\pos\y, boid(i)\pos\z
Next
;***************************************
;*************** STAGE *****************
;***************************************
;;;; camera
Graphics3D 800,600,0,2
SetBuffer BackBuffer()
cam = CreateCamera()
PositionEntity cam, 0,0,-50
CameraClsColor cam, 255,255,255
Repeat... KeyHit(1)
Repeat
CameraClsMode cam, True, True
RenderWorld
Print boid(2)\velocity\x
Print boid(2)\velocity\y
Print boid(2)\velocity\z
boid(2)\pos = norm(Rnd(50),Rnd(50),Rnd(50))
Print boid(2)\pos\x
Print boid(2)\pos\y
Print boid(2)\pos\z
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END PROCESS
Flip()
Until KeyHit(1)
End
;****************************************
;********* FUNCTIONS & TOOLS **********
;****************************************
Function norm.vector(x1#,y1#,z1#)
Function norm.vector(x1#,y1#,z1#)
Local temp.vector = New vector
n=Sqr((x1^2)+(y1^2)+(z1^2))
If x1<> 0.0 Then
sx# = x1/n
Else
sx# = 0.0
End If
If y1<> 0.0 Then
sy# = y1/n
Else
sy# = 0.0
End If
If z1<> 0.0 Then
sz# = z1/n
Else
sz# = 0.0
End If
temp\x = sx
temp\y = sy
temp\z = sz
Return temp
End Function
------------------------------------EOF
to do code highlighting enclose your code in <code> </code> tags (but replaced the <> with [ ])