PBFX V1.76 Beta #9b - Return Arrays From Functions
Just got this working in PBFX, it allow you create an array into a Function, then return this.
Some examples
Integer Arrays
Dim This(1)
; create new array and assign it to the THIS() array
This()= NewArray(20,1000)
; display it's contents
For lp=1 to GetArrayElements(This())
print This(lp)
next
Sync
Waitkey
Function NewArray(Size,RandomRange)
Dim Me(Size)
For lp=0 to Size
me(Lp)=rnd(RandomRange)
next
EndFunction Me() as integer
Float Arrays
Dim This#(1)
; create new array and assign it to the THIS() array
This#()= NewArray(20,1000)
; display it's contents
For lp=1 to GetArrayElements(This#())
print This#(lp)
next
Sync
Waitkey
Function NewArray(Size,RandomRange)
Dim Me#(Size)
For lp=0 to Size
me#(Lp)=rnd(RandomRange)
next
EndFunction Me() as float
String Arrays
name$="Bill,Kev,Test,Dude"
Dim This$(1)
; create new array and assign it to the THIS() array
This$()= NewArray(20,name$)
; display it's contents
For lp=1 to GetArrayElements(This$())
print This$(lp)
next
Sync
Waitkey
Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)
Dim Me$(Size)
For lp=0 to Size
me$(Lp)=NamesList$(rnd(Count))
next
EndFunction Me() as string
Typed Arrays
Type Vector2D
X#,y#
EndType
Type Stuff
name$
pos as vector2d
speed as vector2d
EndType
Dim This(0) as Stuff
; create new array and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"
This()= NewArray(20,name$)
; display it's contents
For lp=1 to GetArrayElements(This())
print This(lp)
print This(lp).Name$
print This(lp).pos.x
print This(lp).pos.y
next
Sync
Waitkey
Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)
Dim Me(Size) as stuff
For lp=0 to Size
me(Lp).name=NamesList$(rnd(Count))
me(Lp).pos.x=100+lp
me(Lp).pos.y=200+lp
next
EndFunction Me() as stuff
Typed Variable
Typed variables are basically typed arrays with 1 element. So you can mix and match assignments, don't confuse this with passing a UDT element..
Type Vector2D
X#,y#
EndType
Type Stuff
name$
pos as vector2d
speed as vector2d
EndType
Dim This as Stuff
; create new typed VARIABLE (array) and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"
This()= NewArray(20,name$)
; display it's contents
print This
print This.Name$
print This.pos.x
print This.pos.y
Sync
Waitkey
Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)
Dim Me as stuff
me.name=NamesList$(rnd(Count-1))
me.pos.x=100+lp
me.pos.y=200+lp
EndFunction Me.stuff
This version returns a Typed Variable, back into a Type Array. Here we can see that it is in fact a 1d array with 1 element..
Type Vector2D
X#,y#
EndType
Type Stuff
name$
pos as vector2d
speed as vector2d
EndType
Dim This(10) as Stuff
; create new typed VARIABLE (array) and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"
This()= NewArray(20,name$)
; display it's contents
for lp=0 to GetArrayElements(This())
print This(lp)
print This(lp).Name$
print This(lp).pos.x
print This(lp).pos.y
next
Sync
Waitkey
Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)
Dim Me as stuff
me.name=NamesList$(rnd(Count-1))
me.pos.x=100+lp
me.pos.y=200+lp
EndFunction Me.stuff