Recycling Bullets
specialist_3
Member Posts: 121
Instead of spawning bullets (which drains memory), I have been trying to recycle a set of PRESPAWNED bullets over and over again each time player clicks. However, I am not able to get it to work properly. My game logic is as such.
2 Actors - Gun, Bullet
Inside Gun,
1) Set Gun's position to Global Attribute
2) When Player Clicks, Game.Count = Game.Count + 1, where Game.Count is Bullet Number
3) When Player Clicks, Set Game.Fired = True
4) When Player Releases, Set.Game.Fired = False
Inside Bullet,
1) IF Game.Fired = True (Player Fired)
AND Self.Life > 0 (Bullet Lifetime)
AND Self.Count = Game.Count, (Bullet Sequence Number)
AND Self.Fire = False (Bullet has not been fired yet)
- Alpha = 1
- Set Position to Gun's Position
- Set Self.Fired = 1
- Interpolate Life to 0
- Timer (FOR) Lifetime, MOVE Bullet
2) IF Self.Life <= 0,
- Reset Life = 2
- Reset Position to Gun's
- Self.Fired = 0 (NOT FIRED)
- Alpha = 0
3) Initialize Alpha & Position at Start of Game
I have encountered 2 problems here.
First, without the FOR Timer Loop for MOVE, when the bullet dies, it sets back to gun's position and continues to move in same direction (with alpha = 0) till it goes off screen and gets destroyed. After that I cannot reuse the bullet. Can this be fixed without having to use Timer?
Secondly, when I fire the bullet, the recycling only works for a couple of cycles and is as follows: 1, 2.....1, 2.....1...1...STOP OR 1, 2...1, 2...1, 2....2, 2, 2...STOP. By STOP I mean Bullets stop responding (not move) after those few cycles. Can someone help me please?
Cheers!
2 Actors - Gun, Bullet
Inside Gun,
1) Set Gun's position to Global Attribute
2) When Player Clicks, Game.Count = Game.Count + 1, where Game.Count is Bullet Number
3) When Player Clicks, Set Game.Fired = True
4) When Player Releases, Set.Game.Fired = False
Inside Bullet,
1) IF Game.Fired = True (Player Fired)
AND Self.Life > 0 (Bullet Lifetime)
AND Self.Count = Game.Count, (Bullet Sequence Number)
AND Self.Fire = False (Bullet has not been fired yet)
- Alpha = 1
- Set Position to Gun's Position
- Set Self.Fired = 1
- Interpolate Life to 0
- Timer (FOR) Lifetime, MOVE Bullet
2) IF Self.Life <= 0,
- Reset Life = 2
- Reset Position to Gun's
- Self.Fired = 0 (NOT FIRED)
- Alpha = 0
3) Initialize Alpha & Position at Start of Game
I have encountered 2 problems here.
First, without the FOR Timer Loop for MOVE, when the bullet dies, it sets back to gun's position and continues to move in same direction (with alpha = 0) till it goes off screen and gets destroyed. After that I cannot reuse the bullet. Can this be fixed without having to use Timer?
Secondly, when I fire the bullet, the recycling only works for a couple of cycles and is as follows: 1, 2.....1, 2.....1...1...STOP OR 1, 2...1, 2...1, 2....2, 2, 2...STOP. By STOP I mean Bullets stop responding (not move) after those few cycles. Can someone help me please?
Cheers!
Comments
When Game.Count > Game.Max.Bullet.Count,
Game.Count = 1
Player
When player fire
set game.fire=1
bullet
when game.fire=1
put the bullet to position
move the bullet
set game.fire=0
(don't need to think which bullet you use, anyway only one bullet will fire)
That will only work in a single bullet scenario not multiple.
The game won't know which bullet to fire without ID. So if you have 2 or more bullets, all will start moving.
you should think each bullet is running as the same time like networked, only one bullet will fire, check it out!
1) IF Game.Fired = True (Player Fired)
AND Self.Life > 0 (Bullet Lifetime)
AND Self.Fire = False (Bullet has not been fired yet)
I removed Self.Count = Game.Count, (Bullet Sequence Number)
I have 2 bullets. This causes both to move at the same time.
I have that already. Check under Gun Actor. When Mouse is Clicked, Game.Fired = True, Not clicked, Game.Fired = False.
Remember I am not spawning the bullets which act independently of each other in which case the count comparison is not required. I have prespawned 2 bullets and set Self.Count = 1 for bullet 1 and Self.Count = 2 for bullet 2.
So when Player clicks, Game.Count (initially 0) is set to Game.Count = Game.Count + 1 and the bullet with Self.Count = 1 is fired. Again when player clicks, count is 2 and second bullet is fired. And when Count is above 3, its reset to 1 and first bullet is fired again. This works perfectly only for 2-3 times and after that it doesn't.
http://gamesalad.com/p/victorkin11/games
try it again!
My template is just one min test, not finish yet, you need to set the bullet.fire=0 when it is outside the screen or hit the target. so that bullet can reuse again.
What I have done is for testing, I limited the MOVE TO with in the screen.
Also, when I click fast, the same bullet keeps getting used again. And when I speed-click, the next bullet gets used but here and then, the bullets seem to travel off-screen which I am not sure why. But I guess I can fix that with a Rule to check if they travel too far or with Life.
What I have found is that the complexity of the bullet actor is the most important factor. Keep it simple. As few rules as possible.
My concern is not so much on the FPS but the RAM.
With the constant spawning & destroying, it starts creep up on the memory usage.
One thing I would like to clarify, in the event I have a gun that rotates, the bullets have to fly in the angle of the gun's rotation. When I worked on your earlier template, if gun is facing 90 degrees and fires, bullet 1 travels at 90 degrees. when I immediately fire bullet 2 at 180 degrees, bullet 1 also changes direction mid way. This is because in my MOVE TO behavior, I have set the X and Y coordinates to touch1.X and touch2.Y. So each touch is updating all the bullets.