Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Erasing An Enemy From Enemy Class  (Read 2614 times)

0 Members and 1 Guest are viewing this topic.

josephdauntless

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Erasing An Enemy From Enemy Class
« on: April 23, 2021, 03:51:54 am »
Hi everyone. i have a problem. I learn to coding and making little projects for improving my skills. I wrote a code like this:

//enemy class
class enemy {
public:
   sf::RectangleShape rect;
   float bottom, left, right, top;
   bool touch = false;
   sf::Sprite itemss;
   sf::Texture enemyt;
   enemy(sf::Vector2f position, sf::Vector2f size, sf::Color color) {
      rect.setPosition(position);
      rect.setSize(size);
      rect.setFillColor(color);
      enemyt.loadFromFile("inv/invpouch.png");
      enemys.setTexture(enemyt);
   }
   void kill()
   {
      std::cout << "Killing Enemy >> " << std::endl;
   }
   ~enemy() {

   }
   void update() {
      bottom = rect.getPosition().y + rect.getSize().y;
      left = rect.getPosition().x;
      right = rect.getPosition().x + rect.getSize().x;
      top = rect.getPosition().y;
      if (right > recx && left < recx + 30 && top < recy + 30 && bottom > recy) {
         enemys.setColor(sf::Color(255, 255, 255, 155));
      }
   }
   bool collision(enemy e) {
      if (right < e.left || left > e.right || top > e.bottom || bottom < e.top) {
         return false;
      }
      return true;
   }
};

    //create enemies
   std::vector <enemy*>  enemies1;
   for (unsigned i = 0; i < 5; ++i) {
      enemies1.push_back(new enemy(sf::Vector2f(100 * i + 800, 500), sf::Vector2f(15, 10), sf::Color(255, 255, 255, 0)));
   }

    //update
    for (unsigned i = 0; i < 5; ++i) {
            enemies1->update();
        }
      
      if (!enemies1.empty()){
         enemies1.erase(std::remove_if(enemies1.begin(), enemies1.end(), [](const auto &itr) {return itr->touch; }), enemies1.end());
        }
      
    //render
    for (unsigned i = 0; i < 5; ++i) {
            window.draw(enemies1->enemysprite);
        }

my goal is erase some enemies. with this code i can do that but problem is that
1- i cant erase last enemy of enemies1 vector. 1,2,3,4th values are erasing but not number 5(array no 4)
2- Using vectors Is a good way for create and delete enemies because in my game there will be some npc's and they will spawn, die and they will have their own stats and such.
I appreciate for every advice.
« Last Edit: April 23, 2021, 03:55:22 am by josephdauntless »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Erasing An Enemy From Enemy Class
« Reply #1 on: April 23, 2021, 07:52:14 am »
I haven't used std::remove_if before, but based on the docs you shouldn't need the erase part.
std::remove_if does the erase internally, then typically returns the end() iterator. That means the erase is going to be trying to erase starting at the end() (which isn't a valid element), so the last element will have trouble.
Try this instead (same code but with erase taken out):
std::remove_if(enemies1.begin(), enemies1.end(), [](const auto &itr) {return itr->touch; });

josephdauntless

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Erasing An Enemy From Enemy Class
« Reply #2 on: April 23, 2021, 06:12:00 pm »
Thanks for answer its working but i m getting same problem. Last element of array does exist i want to erase all :/

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Erasing An Enemy From Enemy Class
« Reply #3 on: April 23, 2021, 07:39:07 pm »
How is touch being set on enemies? It's defaulting to false but isn't used again in the code above except to select which ones to remove. It looks like one enemy isn't having touch set to true, so it won't be removed.

josephdauntless

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Erasing An Enemy From Enemy Class
« Reply #4 on: April 23, 2021, 10:22:00 pm »
thanks for advice. i learned this code from a website and with searching and listening from persons who gave me advices like you i m still trying to learn. i will do something thanks best regards :) if i success to make a game prototype then i will share on game section of forum.

 

anything