type vs array

Blitz3D Forums/Blitz3D Beginners Area/type vs array

Frog(Posted 2008) [#1]
This is my first time to these forums. so I would like to say hello to the community.
Hello
My first question is an easy one.

when do you use arrays and when do you use types?
how do types store data differently then arrays?

basically im asking the differences between the two.


Thank you for any responses


Matty(Posted 2008) [#2]
Types/Lists can change the number of items, simply by creating new instances or deleting instances.

Arrays are able to be re dimensioned but this loses all information in the array. Arrays also are global in blitz.

Banks are another feature of blitz which are very powerful.

Types allow collections of properties for an 'object' and of different data types.

So you could have a type instance (like a struct in c) which has fields for 'name$' as a string, 'hitpoints' as an integer and 'radius#' as a float for example.

In an array the data type is declared for the whole array. So using the example above you would need an array of type string, integer and float - 3 arrays in total.

Types are flexible and easy to use and the performance hit on PCs greater than a Pentium 2 is negligible when compared with Arrays (from my own experience).

If you were doing a game like space invaders you would probably use a type for the space ships (friendly & enemy) with fields like "x,y,lives,animationframe,name$"

In my own code I rarely use arrays but that is just my own preference. Some people use arrays rather than types. Whatever you find easiest I guess.


Ross C(Posted 2008) [#3]
Arrays are handy if say, you have a grid and store information in each slot of the grid. Like say minesweeper. you would have a 2d arrays.

Dim grid(10,10)


That would set up an 11 x 11 grid, because 0 counts as a slot. You would then store say a mine or a blank as data in each slot. When the user clicks on a grid space, you look this up in the array.

Types are very powerful though once you get into them.


Frog(Posted 2008) [#4]
In a type how do you call a specific memory location. I know in an arrey all you need to do is call is as a veriable w/ the location in the arrey. In a type the only way I know of to change the data in a memory location is to run through the every memory location in the type.

to clearify, I was trying to to a touch test on two balls in the same type. When I check each object in the type I cannot call that same type inside the loop because the variable of both ships are the same.
The only thing I can think to do is save the x,y location of the object as another veriable name then check the location of location of all the other balls against that.
This seems like a lot of trouble though. could you send me in another direction.


Jasu(Posted 2008) [#5]
This is what I do.
Type mytype
  Field var1%
  Field var2#
End Type

Local mt1.mytype = New mytype
Local mt2.mytype = New mytype

For a.mytype = Each mytype
  For b.mytype = Each mytype
    If Handle(a)>Handle(b) then
      ; do your stuff
    Endif
  Next
Next

You can also do If a<>b , but that will result two tests: mt1 against mt2 and mt2 against mt1. The function Handle() returns the pointer of the type object. I think this is what you wanted?


jfk EO-11110(Posted 2008) [#6]
Personally I try to avoid types as much as possible. Simply because I think it complicates the readability of code. No doubt, types are very useful in some situations. The classic requirement for types is: you have a player with several properties, some ints, some floats and some strings. You couldn't store all this in one DIM array, but it works with a type.

Additionally, as mentioned earlier, arrays can't be redimed dynamicly - at least not by default. You can however emulate this with some simple functions, eg:
new_size=old_size+1
dim helper_array(new_size)
for i=0 to old_size
 helper_array(i)=gamedata(i)
next
dim gamedata(new_size)
for i=0 to old_size
 gamedata(i)=helper_array(i)
next

So the array gamedata was resized pseudo-dynamicly. Unfort. you can't use an array pointer as a function parameter, it however works as descrbed before.

In case you only need a static sized array then you should;t use a type since types invoke a lot address pointer work. Some people use types to create a variable number of objects and then parse them each time they need to access one of them, eg.

for each mytype
comparing some fields with some other fields, just to find out the index of this type
next

Instead you better create an array of types, this way you'll be able to access them by index:
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



Ross C(Posted 2008) [#7]
Use can use the Object and Handle commands to store and quickly retrieve a specific type ojbect. thanks for that jkf, i was trying to remember how to do that with the type array :o)


Frog(Posted 2008) [#8]
that cleared a lot of stuff up. thank you for your responses