Advertisement
Cinestra

Alternate way of cleaning up inactive actors from Project 3

Jun 5th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. size_t i = 0;
  2. while (i < m_actors.size())
  3. {
  4. if (m_actors[i]->is_active() == false)
  5. {
  6. // First delete the actor being pointed to by m_actors[i]
  7. delete m_actors[i];
  8.  
  9. // Then we erase the pointer m_actors[i] from the m_actors vector
  10. // Erase needs an iterator to be given as its parameter to know which element in the vector to erase
  11. // So we will define our iterator and set it equal to m_actors.begin() and add the element number it is in the vector
  12. auto itr = m_actors.begin() + i;
  13. // Notably the erase function actually shrinks the vector and moves all the other elements over
  14. // So we do not need to increase i here
  15. m_actors.erase(itr);
  16. }
  17.  
  18. else
  19. i++;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement