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

Author Topic: Any way I can destroy objects?  (Read 2841 times)

0 Members and 1 Guest are viewing this topic.

AdrianHEY

  • Newbie
  • *
  • Posts: 14
    • View Profile
Any way I can destroy objects?
« on: April 08, 2021, 09:03:07 pm »
I want to make it so if I shoot a bullet and hit something I destroy the bullet and the target. I tried to make it so the object wont draw, but if I don't draw it, it will still be there and kill the enemies so what should i do? I thought of a destructor function of the Bullets class but I have no idea how to use it._.
sorry for the bad english

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Any way I can destroy objects?
« Reply #1 on: April 09, 2021, 10:40:04 am »
Even if you would call the destructor, the object would still be there, just in a destroyed state. But you'd still have a variable referring to it. Also it would be undefined behavior for stack/automatic objects, their destructor is called at the end of their scope, so don't invoke it manually.

What you typically want to do for things like bullets is a STL container, almost always std::vector (see cppreference.com). You can dynamically add and remove objects, and iterate over all existing objects. So whenever someone shoots a bullet, you add it, and when it hits something, you remove it from the vector.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything