Recycling Bullets

specialist_3specialist_3 Member Posts: 121
edited November -1 in Working with GS (Mac)
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!

Comments

  • specialist_3specialist_3 Member Posts: 121
    I reset the bullet count as follows:

    When Game.Count > Game.Max.Bullet.Count,
    Game.Count = 1
  • victorkin11victorkin11 Member Posts: 251
    My though

    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)
  • specialist_3specialist_3 Member Posts: 121
    @victorkin11,

    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.
  • victorkin11victorkin11 Member Posts: 251
    no! only one bullet will get the game.fire signal!
    you should think each bullet is running as the same time like networked, only one bullet will fire, check it out!
  • victorkin11victorkin11 Member Posts: 251
    don't use the normal programing logic in GS, every actor have them own running time!
  • specialist_3specialist_3 Member Posts: 121
    Ok I will give it a try now.
  • specialist_3specialist_3 Member Posts: 121
    Ok, so Inside Bullet,

    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.
  • victorkin11victorkin11 Member Posts: 251
    When the bullet get the signal, it need to set Game.Fired=False, so other bullet won't get that signal!
  • specialist_3specialist_3 Member Posts: 121
    @victorkin11

    I have that already. Check under Gun Actor. When Mouse is Clicked, Game.Fired = True, Not clicked, Game.Fired = False.
  • specialist_3specialist_3 Member Posts: 121
    Without Self.Count = Game.Count comparison, the game will not no which bullet to send out.
    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.
  • specialist_3specialist_3 Member Posts: 121
    By "prespawned" i mean, in design mode, I drag 2 instances of the bullet actor and put them on screen.
  • victorkin11victorkin11 Member Posts: 251
    no, inside the bullet, when the bullet get Game.Fired=true set Game.Fired=False & fire that bullet, check the first post My though, I have tested, it work!
  • specialist_3specialist_3 Member Posts: 121
    Can I pass you my file. Please have a look. What's your ID?
  • victorkin11victorkin11 Member Posts: 251
    My sample, not finsh yet, but it work

    http://gamesalad.com/p/victorkin11/games
  • victorkin11victorkin11 Member Posts: 251
    I just reupload it again, so you can download the project!
    try it again!
  • specialist_3specialist_3 Member Posts: 121
    Thanx...will try it now
  • specialist_3specialist_3 Member Posts: 121
    Its gonna take me a little while to get back on this. I am working on your template to include life of bullet, alpha and such. Thanks for the help.
  • victorkin11victorkin11 Member Posts: 251
    You are wellcome!
    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.
  • specialist_3specialist_3 Member Posts: 121
    Yea, I noticed that. But its cool. I was not aware the actors use game.time as count.

    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.
  • ORBZORBZ Member Posts: 1,304
    I've noticed that in the more recent builds of gamesalad that spawning and destroying are not as heavy as they used to be. I have a prototype game with 50+ bullets spawning and destroying and the framerate is 40fps on an old iPhone 3G.

    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.
  • specialist_3specialist_3 Member Posts: 121
    Hi ORBZ,

    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.
  • victorkin11victorkin11 Member Posts: 251
    I just update my sample!
  • specialist_3specialist_3 Member Posts: 121
    This is amazing. Here I am trying to complicate things with Lifetime and alpha...
    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.
  • specialist_3specialist_3 Member Posts: 121
    I am doing some additional test. I also check if it crosses X, -X, -Y. If I continue to fire at rapid rate, at a certain stage, the number of reusable bullets reduces to 1 and after sometime, that too stop suddenly half way and does not respond to the clicks.
  • chosenonestudioschosenonestudios Member Posts: 1,714
    specialist_3 said:
    Hi ORBZ,

    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.

    Are you testing this with the viewer? There are many known problems with the viewer and there might not actually be a memory creep on your device. I'd check it out.
  • specialist_3specialist_3 Member Posts: 121
    I check it on actual device. Continuous spawning slowly eats up memory. Destroying doesn't seem to efficiently deallocate the memory.
  • chosenonestudioschosenonestudios Member Posts: 1,714
    specialist_3 said:
    I check it on actual device. Continuous spawning slowly eats up memory. Destroying doesn't seem to efficiently deallocate the memory.

    It seems there working on this and it should be fixed next update:D
  • specialist_3specialist_3 Member Posts: 121
    man if that issue is solved...nothing like it. i have 2 games in the pipeline (each 60% done) but shelved because of this spawning problem.
Sign In or Register to comment.