Arrays vs Lists

BlitzMax Forums/BlitzMax Beginners Area/Arrays vs Lists

Lillpeter(Posted 2005) [#1]
I´m currently working on my first Bmax game and i´ve run into a qouple of questions.

1. In my game i´m using an array (consisting of types) to hold the bullets i create when playing (reorganizing it to fill the empty slots as the bullets die).
While for all my particles that i generate when i blow something up i´m using a linked list to hold them.
I´m wondering wich technique that is best suited for holding all the objects? i mean i could easily create a linked list for the bullets instead and vise versa for the particles.

2. My second question is about drawing all all the stuff to the screen, as i generate alot of particles (about 800 - 1000 particles) they really slows the game down, any tips on improving drawing times would be appreciated.

Maybe it´s hard for you to answer these questions without looking at my code, but i´m mostly interested
in general priciples for programming (in blitzmax) i don´t really know anything about optimizing yet so any tips on this would be very helpful.

Me out

Peter


Diablo(Posted 2005) [#2]
i would always use a list for bullets. if you want to limit the number of bullets just do somthing like

if bulletlist.count() < 100 Then bulletlist.addlast mybullet.create(x, y, whatever)

I would also use a list for the particles as well, because lists are dynamic and arrays are static, you can easily add and remove items.

making it draw the particles faster will depend on your looping structure. make sure your not doing the same thing over again. You could also try reducing the number of colours the particle sprites use, or make them smaller


Dubious Drewski(Posted 2005) [#3]
Hmm, I don't see how 800 - 1000 particles could slow your
program. Even if they were drawn via bitmaps. (I recently
made a program that had 16,000 particles bouncing and
flying around my screen. I had Bmax cap it off at 60 fps)

Can you loosely explain how your particles move/react to the world?

And the general rule seems to be that lists are cleaner and
have some nice functions, while arrays are simpler and run
faster, but also require a bit more work for you.


Dubious Drewski(Posted 2005) [#4]
Diablo, Arrays are dynamic too. can you run this and tell
me if this is what you are talking about?
Strict

Local array:Int[20]
array[19] = 99

array = New Int[10]

Print array[19]



this program will crash because you changed the array
size. This is fine, but the problem is, you lose the data the
array was holding, but the array gets resized! You could
just create a temporary array to hold the data while you
resize the main array. And there you have a dynamic
collection of things with the speed advantages of Array and functionality of TList.


Diablo(Posted 2005) [#5]
yes but you still have to tell it how big you want the array. and i get an output of 0 anyhow.

EDIT: you thinking that it adds 10 because it dosen't


Dubious Drewski(Posted 2005) [#6]
I know, it takes 10 away. : ) That's why it crashes.


Dubious Drewski(Posted 2005) [#7]
[double post]


Lillpeter(Posted 2005) [#8]
wow, thanks for the fast replies

Drew

as for the particles they don´t really interact with anything,
they are just thrown outwards by random angles, and they also contain some other simple data, here´s the type i´m using.

Type spark
Field x#,y#,move_x#,move_y#,r#,g#,b#,life#,alpha#,scale#
End Type

the move_x and move_y variables are added to the x and y when moving the particle.


Diablo(Posted 2005) [#9]
no, it just assigns array as a new 10d array with empty pointers. which means you loss all info, if you are going todo it that way you have make sure you create a temp array, fill that array, and then resize the new array and fill it with the items in the other array:

Strict
Local i:Int
Local c:Int

Local array:Int[20]
array[9] = 99
Local tmparray:Int[20]
For i:Int = EachIn array
tmparray[c] = i
c:+ 1
Next
c = 0

array = New Int[10]
For i:Int = EachIn tmparray
array[c] = i
c:+ 1
Next
c = 0


Print array[9]

;; which isn't dynamic (because you create a new array instead of resize it)


Diablo(Posted 2005) [#10]
@Lillpeter

make sure you delete them when they go out of view.

EDIT: like

for aspark:spark = eachin sparklist
if aspark.x < 0 or aspark.x > graphicswidth() or aspark.y < 0 or aspark.y > graphicsheight() then sparklist.remove(aspark)
next


Dubious Drewski(Posted 2005) [#11]

empty pointers. which means you loss all info



Yeah, I said that. ;)
And sure it's not truly dynamic, but the tmp method works fine.
And the speed benefits of arrays over lists is worth
considering. To be honest, I don't even consider using lists
when I program. There is no benefit to make up for the
speed loss.

Worst case scenario with arrays:
If you are feeling lazy and don't want to be bothered to
write an array-resizing function, you can just set a high
upper limit to your arrays.

Say for example, you're doing effects particles. You should
always have an upper limit anyway (say, 1000) so
initialize 1000 particles but only process active ones.

for local a = 0 to particleNum

to add a particle:

if particleNum < particleMax particleNum:+1
particle[particleNum+1] = active

to remove any arbitrary one:

particle[a] = particle[particleNum]
particleNum:-1

Believe me, the above methods are invaluable and quick


Lillpeter(Posted 2005) [#12]
Yo,
all the bullets, enemys and particles are deleted when they exits the screen, i´m writing out the variable values on the screen as i play and it works fine.

Also, i downsized my particle image, i really seems to run faster, it was way to big anyway, thanks for all the tips.


tonyg(Posted 2005) [#13]
Diablo/Drew,
You might like to look at slices to resize arrays
Local a[10]	'initialize a[] to 10 elements
For x = 0 To 9
  a[x]=x
  Print a[x]
Next
a=a[..5]	'resize a[] to 5elements
For x=0 To (SizeOf a / 4) -1
	Print a[x]
Next

Check Speed Optimization Hints for particle hints although 800-1000 suggests something else is wrong.
Personally, I would use lists for both bullets and particles as you'll be adding and deleting both quite often. I would use arrays (or arrays of types) for more static data or for direct access (e.g. tiles).
I'm sure there was big discussion about lists vs arrays here a couple of months ago.
List speed
Lists vs arrays
lists vs arrays 2
Array of types


Dubious Drewski(Posted 2005) [#14]
Ahhh, array slicing. Nice! Thank you Tonyg, I forgot about that.