Arrays....

Blitz3D Forums/Blitz3D Beginners Area/Arrays....

Hybird(Posted 2003) [#1]
Hi! I just have a completely newbie question. What are arrays? While I realise they're something you put variables in or something like that, I still find grapsing the concept of an array difficult. May someone please explain what it is in a little detail and what they are commonly used for in games programming? Thanks!


Mustang(Posted 2003) [#2]
http://members.lycos.co.uk/dbasic/array_tutorial.htm


Ross C(Posted 2003) [#3]
think of mine sweeper. i'm sure that uses an array. the way i think of it is a grid. an array can have many dimentions.

it basically allows you to refer to a variable dynamically.

think of the minesweeper game.

dim minegrid(10,10)


this will basically set up an array (a grid) 10 wide by 10 high.

this has many advantages. 1st being if you want to clear the grid for some reason, you would use 2 loops to clear all the values.

for loop=0 to 10
   for loop1=0 to 10
      minegrid(loop,loop1)=0
   next
next


this would clear the entire grid. think of doing that not using arrays. would be very painful

minegrid_0_0=0
minegrid_0_1=0
minegrid_0_1=0
minegrid_0_2=0
....
....


would take alot of lines to do that, never mind accessing a particular variable. hope some of this helps, mustangs link is very good too :D


Hybird(Posted 2003) [#4]
Thanks, very helpful.


Jager(Posted 2003) [#5]
I find it really strange that Arrays must be global?

That restricts them from being put in Types or functions?

Yes, I know i can use linked lists (types) but I want to be lazy! :)


SoggyP(Posted 2003) [#6]
Hi Folks,

@Jager: You can put arrays into types but only single dimensioned ones. You have to use the square brackets as well so:

Type Test_Type
 field id
 field array[256]
end type


Please note, that Blitz Arrays(as they are known) start at 1, not 0.

You can have arrays of types within types so
type Test_Type
 field id
 field cells.cells_type[256]
end type


though you have to create each array member when you want to use it as you would a normal type. Also, be aware there are some major speed issues when freeing(and creating?) type arrays within types.

If you need multi-dimensional arrays within types you have to work it out for yourself, so in the above if we were talking about a 16 * 16 grid (hence 256 elements) we'd have to go

x = 10
y = 10
element = (y * 16) + x
v = test\array[element]


Hope that's of some use.

Later,

Jes