Bullet Help

Blitz3D Forums/Blitz3D Beginners Area/Bullet Help

mintybreath(Posted 2008) [#1]
I feel kind of funny asking what i think is a simple question. Ive been away from coding for a little while, and cant seem to remember the answer.

So, how do i make a simple bullet system using types?
I have a gun, and the bullets types. I got it to move when i press space, but it only moves while space is down, and instead of making a new bullet when repressed, it moves the old one back.
I heard using types was more efficient than using arrays, but if it isnt, then please tell me.


If arrays is better, then how do you make a working gun system using arrays?

Again, i feel silly, like i should surely know how to do this, but some help would be really great.


jfk EO-11110(Posted 2008) [#2]
If arrays or types are better depends on the requirements and the implementation. Types tend to be much slower when not used properly. It doesn't make sense to use For Each loops, only to seek a certain index, what a lot of people yet seem to do. So for a bullet system you'll need for sure not only types, but arrays of types!

Bullet system implementations have been discussed many times and I suspect you didn't use the forum search so far.

I would however suggest to learn about types and/or arrays, as well as the needs of a bullet system.

Here's a good sample about arrays of types.
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

While this code allows to access your types by index directly, it lacks of a feature that normally is a positive
Attribute of types: you can create new ones and delete them, without to manually manage indicies, low- and high-marks etc.

As a teaching project I'd recommend to code it with plain arrays, just to understand every aspect of what's going on and what has to be done.


So, there is a number of bullets that should act independently and parallely. This means, each bullet requires some variables:

bullet handle
bullet speed
bullet state maybe

So these would be 3 Arrays

there will be two functions, shoot_bullet and update_bullets.

shoot_bullet creates a bullet mesh by the hit of the firebutton at the players position, stores the handle in an array, rotates it to the aiming direction and sets the speed value in the speed array
to the max. It will then increase the number of active bullets variable.

UpdateBullets will be called in the main loop. It will use a FOR loop with the active bullet counter and iterate trough all bullets. It will MoveENtity every bullet by the value taken from the speed array. Collision is detected. THe speed value is decreased (eg. speed=speed*0.95). Now if there was a collision, or if the speed reaches a minimum, then we want to remove this bullet from the list of active bullets: simply FreeENtity the bullet handle and then remove the array entry from the arrays. While types would make this easier, it still isn't rocket science with arrays:

let's say the array-index of the to-be-removed bullet is 10, and there are 30 active bullets in total, we would do something like this:
remove_this=10
max_index=30

for i= remove_this to max_index
  bullet(i)=bullet(i+1)
  speed(i)=speed(i+1)
  ;...
next
max_index=max_index-1


Then you still have to limit things in order to stay between zero and the dimension of the arrays, that may be the max number of simultanous bullets in the air.