+ 1
How to delete a class object with a function
So I'm writing a game in python 3 where the enemies are class objects. When an enemy dies I have no more use for the object so to remove it from play I want to delete it. I wrote a function that checks the hp of each enemy but how do I actually delete the object? I've attached an example that DOESN'T work. What should I be doing instead? https://code.sololearn.com/cF1gYGY1iDRV/?ref=app
4 Answers
+ 7
you have to define destructor for class enemy
consider this code snippet,
class enemy:
#your other functions......
def __del__(self):
#del calls for other member objects of
# enemy if any
#you will delete object of type enemy by
del objOfTypeEnemy
+ 6
Erik N you can ping me if you want my help again.
how to ping:-
comment on my code
or
you can type '@' then my name, I will get notified that you mentioned me
+ 6
but my only condition for helping you is, you should try yourself first , Google , and ask me if you still don't understand.
I will try to help you
+ 1
Thank you for teaching me something new. After reading about destructors and garbage collection I've decided that I asked the wrong question and that I don't really know what I want to do yet lol.
However, I think I can use destructor functions to figure out what I want to ask so thank you!