Skip to main content
Tweeted twitter.com/StackGameDev/status/1184665517644304385
added 654 characters in body
Source Link

Edit: Bullets are generated right now on a loop within a enemy class

void shoot(int which){
    Bullet B1;
    B1.setsprite();
    currentangle+=(6.2832/bnum)+0.05;
        B1.setvars(x,y,currentangle*which,1.f,2.f,0.01f);
    Bulletlist.push_back(B1);
    }
    void update(){
     movtimer+=0.01f;
     //x += sin(movtimer);
     //std::cout<<x;
     //printf("\n");
     sprite.setPosition(x,y);
      if (timer>=10){
      for (int i=0; i<bnum; ++i) {
      shoot(1);   
      }
      timer=0;
      }else{
      timer++;
      }
      window.draw(sprite);
      for (std::vector<Bullet>::iterator it = Bulletlist.begin() ; it !=    Bulletlist.end(); ++it){
       it->update();
      }
```

Edit: Bullets are generated right now on a loop within a enemy class

void shoot(int which){
    Bullet B1;
    B1.setsprite();
    currentangle+=(6.2832/bnum)+0.05;
        B1.setvars(x,y,currentangle*which,1.f,2.f,0.01f);
    Bulletlist.push_back(B1);
    }
    void update(){
     movtimer+=0.01f;
     //x += sin(movtimer);
     //std::cout<<x;
     //printf("\n");
     sprite.setPosition(x,y);
      if (timer>=10){
      for (int i=0; i<bnum; ++i) {
      shoot(1);   
      }
      timer=0;
      }else{
      timer++;
      }
      window.draw(sprite);
      for (std::vector<Bullet>::iterator it = Bulletlist.begin() ; it !=    Bulletlist.end(); ++it){
       it->update();
      }
```
Source Link

C++ Object management/deletion

I am working on a game with a lot of bullets, currently every second it loops through a vector of bullet objects and checks if its marked for erasure or not.

if (timer2>=120){

    auto it = Bulletlist.begin();
    while (it != Bulletlist.end())
    {
    if ((it->del)) {
             it = Bulletlist.erase(it);
         }  
         else {
             ++it;
         }
    }
    timer2=0;
    }else{
    timer2++;
    }

This works fine for around 0-200 bullets, however when I try to scale it larger the delay becomes very noticeable. I am still very new to C++, And was curious if there is a better way of doing this. The bullet class automatically marks itself for deletion when it goes out of bounds.

    int update(){
    if (acceleration!=0){
    if (speed < acceleration){
    speed+=increment;
    }
    }
    x=(speed*cos(angle)) + x;
    y=(speed*sin(angle)) + y;

    if (x<0) del=true;
        if (x>1000) del=true;
        if (y<0) del=true;
        if (y>750) del=true;
    circ.setPosition(x,y);
    window.draw(circ);
};