Create unlimited objects

Hi,

I am currently writing a game where the player can shoot projectiles.
I want the projectile to be its own class and create a new object every time i shoot.
I know how i can manually create a new object but my question is how i can create an unlimited number of instances.

thanks in advance

1 Like

I have built a couple of games that allow you to shoot numerous times . due to memory constraints, I suspect that you might not really need to have an unlimited number of shots but a large, fixed number. You can store the instances in an array (or other structure like a queue).

I created a simple struct:

struct Bullet {
  uint8_t x;
  uint8_t y
  bool active;
}

And an array of Bullets:

Bullet bullets[10];

When I want to fire a bullet, I circle through the elements of the array and find one that is inactive and use it.

You can save memory by removing the ‘active’ property by assigning a value of (say) 255 to either the X or Y value to indicate that it is inactive.

3 Likes