playMaker

Author Topic: How to limit the number of objects created  (Read 1950 times)

Micah

  • Playmaker Newbie
  • *
  • Posts: 30
How to limit the number of objects created
« on: March 26, 2018, 07:31:40 PM »
I am doing shooting mechanics. I can spawn bullets, change the rate of fire and the bullets destroy themselves if hey collide with something. I want to however, limit the amount of bullets on screen.  So if there are 3 bullets alive, then you can't shoot another one until one dies etc. I was wondering what Action I should use?

skipadu

  • Playmaker Newbie
  • *
  • Posts: 43
  • Learning continuously
    • serialkamikaze.itch.io
Re: How to limit the number of objects created
« Reply #1 on: March 26, 2018, 11:02:30 PM »
There are many possible choices to do that, but maybe this is a simple one to start with.

Use one integer to store the current amount of ammo on the screen. This can be any name you want, in this example, it is maxAmmo
0 -> no ammo on screen
1, 2, 3 -> ammo count on screen

Then allow launching of new ammo if maxAmmo < 3
Not currently on my own computer to check, but the name for this action might be Int switch

Then you update that maxAmmo value whenever you create new one or destroy one.

Micah

  • Playmaker Newbie
  • *
  • Posts: 30
Re: How to limit the number of objects created
« Reply #2 on: March 29, 2018, 02:44:20 PM »
Do I have to give the bullet prefab the same int variable as well?

Deek

  • Full Member
  • ***
  • Posts: 133
Re: How to limit the number of objects created
« Reply #3 on: March 29, 2018, 04:03:52 PM »
No, that shouldn't be necessary.

You can use a global variable to store the amount of spawned bullets (in the "Variables"-Tab right-click on the int variabel and move it to the globals, if not already).

Wherever you spawn your bullets, you check beforehand if that global int exceeds the limit with the action 'Int Compare' and only continue if it doesn't.
After you spawn a bullet you increment that global int with 'Int Add' and where your bullets get destroyed you decrement that variable also with 'Int Add' but setting the value in this action to -1.

You might want to reset that global int to 0 when the scene loads, to make sure that this counter starts fresh.
If you spawn and destroy the bullets in the same FSM, you could also use a local int variable.

Micah

  • Playmaker Newbie
  • *
  • Posts: 30
Re: How to limit the number of objects created
« Reply #4 on: March 29, 2018, 07:58:44 PM »
It seems globals don't work properly on prefabs

I know it works at least once because when I play the scene, the int is -1 instead of Zero. Other wise, it just adds until the limit, then I can shoot anymore.
« Last Edit: March 29, 2018, 08:30:29 PM by Micah »

Micah

  • Playmaker Newbie
  • *
  • Posts: 30
Re: How to limit the number of objects created
« Reply #5 on: March 29, 2018, 09:22:06 PM »
Oh my God. I feel like such a fool. I got it working, using global variables with "Int Add."

I've been at this for 5 hours, pulling my hair out. Then I simply messed around while playing with my game and shot at a wall that my bullets could fly through. Then I realized that it was working all of this time. The reason why it appeared that it wasn't working was because of the way the bullets were dying. The FSM I was messing with only included if the bullets died flying, not if they died colliding. All I had to do was add the Int Add to the FSM that did with collision. The answer was staring at me in the face, yet I didn't realize the bullets died in two ways lol sorry guys.
« Last Edit: March 29, 2018, 09:24:30 PM by Micah »