How do I create multiple of the same type?

Blitz3D Forums/Blitz3D Beginners Area/How do I create multiple of the same type?

DNR(Posted 2007) [#1]
using C you could create multiple of the same structs in this manner:

struct data
{
int x, y;
};
typedef struct data data;

data ship[10]

and use them in a for loop like this:

int i;
for (i = 0; i < 10; i++)
{
ship[i].x = 1;
ship[i].y = 1;
}

Is there a way to create multiple types and use them in a for loop in Blitz3D just like I used in the example code for a C program?


big10p(Posted 2007) [#2]
Type dataT        ; 'data' is a reserved word.
  Field x%, y%
End Type

Dim ship.dataT(9) ; 0 to 9, inclusive in blitz.

For i = 0 To 9
  ship(i) = New dataT
  ship(i)\x = 1
  ship(i)\y = 1
Next



DNR(Posted 2007) [#3]
oh, I see thank you big10p.


Sledge(Posted 2007) [#4]
While the code big10p gave you is, as you asked, exactly like the C source, we should probably mention that you can iterate through automatically generated lists of type instances also (which basically cuts out having to use fixed size arrays):


Linked lists are pretty useful for games programming and you'll probably end up adopting them sooner or later.


big10p(Posted 2007) [#5]
Yep. that functionality surprised and delighted me when I first got blitz. Now, if only we could have separate lists of the same type, like bmax... :)


DNR(Posted 2007) [#6]
I'm going to have to try using that on one of my programs. Thanks for the heads up Sledge.


Vorderman(Posted 2007) [#7]
Bear in mind you don't need to DIM an array of types, you can just create loads with the same name and then iterate through them using an EACH loop -
type TYPE_thing
   field x#
end type

;create 10 things
for a=1 to 10
   thing.TYPE_thing = new TYPE_thing
next

;now cycle through them and increment their X positions
for t.TYPE_thing = EACH TYPE_thing
   thing.x# = thing.x# + 1
next


EDIT : just read Sledge's post, which says the same thing. Ah well, never mind :)