bullet mode help

Blitz3D Forums/Blitz3D Programming/bullet mode help

mtnhome3d(Posted 2008) [#1]
i created a nice little function that makes either bullets or missles but when i check the mode value it always seams to crash but only if i'm near a collidable object such as a spaceship if i'm not near an object it just doesn't create a bullet. i call the function using fire(1,1) for a laser and fire(1,2) for a missle. i have read through it once and there doesn,t seem to be any big problems with the code but i might have missed something.
Function fire(src,mode=1)
If src=1 And mode=1
For p.player=Each player 
   LinePick EntityX(p\piv),EntityY(p\piv),EntityZ(p\piv),0,0,-1000
  If PickedEntity>0
   PositionEntity p\apiv,PickedX(),PickedY(),PickedZ()
  Else 
   PositionEntity p\apiv,0,0,-100
   MoveEntity p\apiv,0,0,-10000
  End If
  pnum=1-pnum
  If pnum=1 
   q=p\fpiv1
  Else q=p\fpiv2
  End If
  b.bullet=New bullet
   b\ent=CreateCube(q);LoadSprite("media\laser1.bmp",4,q)
   b\mode=1
   b\x=0
   b\y=0
   b\z=0
  PointEntity b\ent,p\apiv
  EntityParent b\ent,0
  EntityType b\ent,lazer
Next
ElseIf src=1 And mode=2
For p.player=Each player
If p\missle>0
 p\missle=p\missle-1
  b.bullet=New bullet
   b\ent=CreateCube(p\ent)
   b\mode=2
   b\x=0
   b\y=0
   b\z=0
  ScaleEntity b\ent,15,15,15
  PointEntity b\ent,p\apiv
  EntityParent b\ent,0
  EntityType b\ent,lazer
End If
Next

Else If src=2

For e.enemy=Each enemy
  If e\firing=1
  b.bullet=New bullet
   b\ent=LoadSprite("media\laser2.bmp",4,e\ent)
   b\x=0
   b\y=0
   b\z=0
  PointEntity b\ent,e\apiv
  EntityParent b\ent,0
  EntityType b\ent,lazer
 End If
Next
End If
End Function

Function updatefire()
For b.bullet=Each bullet
 For e.enemy=Each enemy
 If b\mode=1
  MoveEntity b\ent,0,0,-1
   If EntityCollided (e\ent,lazer)
    FreeEntity b\ent
    Delete b
    e\hp=e\hp-10
   End If 
 ElseIf b\mode=2
    MoveEntity b\ent,0,0,1
   If EntityCollided (e\ent,lazer)
    FreeEntity b\ent
    Delete b
    e\hp=e\hp-100
   End If
 End If
 Next
Next
End Function



mtnhome3d(Posted 2008) [#2]
i tested it a little more. it loads everything fine but for some reason does not create a whole new instance of the bullet.


Dreamora(Posted 2008) [#3]
i don't see why it shouldn't. your main problem is the parenting. unparent the bullet, you don't want it to turn when the player turns, do you ;-)


mtnhome3d(Posted 2008) [#4]
i unparent the bullet with
EntityParent b\ent,0
so thats not the problem. i can't seem to make any of this work. it has to do with the bullets bieng unofficaly nulled.


Roland(Posted 2008) [#5]
When I've dealt with deleting types in the past and freeing entities and such, I've never had any luck unless i do it this way:

1) for whatever type you need to kill off, such as bullet, add a field called 'die'.

2) in your main loops, if an instance of that type is ready to be killed off, set it's 'die' field to true (b\die = true)

3) finally, at the very end of your main loop in a separate for / each loop, check to see if any of your instances "b\die = true" and if so, free their entities and delete them then... just be sure to free the entities first, then delete the type.


here's some pseudo code:


Type bullet
field entity
field die
end type


while not keyhit(1)

;main update loop

for b.bullet = each bullet
      ;move it, shoot it, etc.
next


for b.bullet = each bullet
     if b\die = true
          freeentity(b\entity)
          delete b
     endif

next

wend




Anyway, give that a shot and see if it helps. Not sure why it never seems to work the other way, but that's usually how i end up having to use it.

cheers,
roland


mtnhome3d(Posted 2008) [#6]
i fixed it mostly. i rewrote it with the differing types of bullets as there own seperate type list. heres the fix.




ignore the explosion function but anyway it seems to ignore the if m<> null check and gives me the error when the bullets collide with the saucers.

<edit> i fixed all the problems and will most likly give it to the community as an example.