bullet mode help

Blitz3D Forums/Blitz3D Programming/bullet mode help

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


i tested it a little more. it loads everything fine but for some reason does not create a whole new instance of the bullet.

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 ;-)

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.

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

i fixed it mostly. i rewrote it with the differing types of bullets as there own seperate type list. heres the fix.

Function fire(src)
If src=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
  ;DebugLog "pnum "+pnum
  End If
  b.bullet=New bullet
   b\ent=LoadSprite("media\laser1.bmp",4,q)
   api_Beep(200,100)
   b\x=0
   b\y=0
   b\z=0
  PointEntity b\ent,p\apiv
  EntityParent b\ent,0
  EntityType b\ent,lazer
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
 MoveEntity b\ent,0,0,-5
 For e.enemy=Each enemy
  If EntityCollided (e\ent,lazer)
   FreeEntity b\ent
   Delete b
   e\hp=e\hp-10
  End If 
 Next
Next
End Function


Function explosion()
For e.enemy=Each enemy
 If e\hp<1
   f.particle=New particle
   f\ent=CreateSprite(e\ent)
   f\x=0
   f\y=0
   f\z=0
   f\life=39
   f\tex=LoadAnimTexture("media\explosion.png",4,64,64,0,39)
   EntityTexture f\ent,f\tex,frame#
   ScaleSprite f\ent,50,50
   EntityParent f\ent,0
   EmitSound explosnd,f\ent
 EndIf
Next
End Function

Function missle()
For p.player=Each player
If p\missle>0
 p\missle=p\missle-1
  m.bullet=New bullet
   m\ent=CreateCube(p\ent)
   m\x=0
   m\y=0
   m\z=0
  ScaleEntity m\ent,15,15,15
  PointEntity m\ent,p\apiv
  EntityParent m\ent,0
  EntityType m\ent,lazer
End If
Next
End Function


Function updatemissle()
For m.bullet=Each bullet
If m<> Null
MoveEntity m\ent,0,0,10
 For e.enemy=Each enemy
    
   If EntityCollided (m\ent,enem)
     FreeEntity m\ent
     Delete m
     e\hp=e\hp-100
   End If
 Next
End If
Next
End Function




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.