skyzzle
skyzzle

Reputation: 187

How do I unload an instance of a class in python

I think it better to give an example on why I would want to do this. Say I had a game. There are some objects that are enemeys these enemeys have a class that is running (health, weapons, ect) If a player kills a enemy I want to unload that enemeys data from the game (removing the enemys instance of the class) How would I go about doing this?

EDIT:

Ok using del dose not help. Here is a basic example Imagination obj1 is a enemy and after i=10 imagination that the enemy dies. I have the code to delete the obj however the function Update() that is in obj1 is still being called as the shell still prints "AAAAAAAAAAAA" even though the shell prints d as True

import threading
import time
import sched
#import Queue

#setting up some OneInstance Var's
starttime=time.time()
condition = threading.Condition()
lock = threading.Lock()

#This is the tick method for calling the update threads in the child classes (set at 20 Ticks/s)
#The tick is run in its own thread so it can not be interrupted
#The tick method is part of the Base module but not part of the base class As there needs to be ONLEY ONE INSTANCE of it
def Tick(cond):
    while True:
        with cond:
            cond.notifyAll() #This clears the block on the threads evey 20th of a second
        time.sleep(0.05 - ((time.time() - starttime) % 0.05)) #This is the 20th of a second part

tickThread=threading.Thread(name = 'ThreadTick', target=Tick, args=(condition,)) #This setsup the Tick in its own thread
tickThread.start()#This runs said thread

#BaseClass is the class All other classes inhearit from
class BaseClass(object):
    def __init__(self, name):
        global condition
        global lock
        self.name = name
        t=threading.Thread(name = 'Thread'+self.name, target=self.UpdateHandler, args=(condition,lock,))
        t.start()


    def BaseClassType(self):
        """Returns a string of the childs type"""
        pass

    def UpdateHandler(self, cond, lock):
        #this part handles when to call the update.
        #it is allso needed so that it can be run in a thread.
        while True:
            self.lock = lock #this is just passed down evey tick so that threads can lock them selfs when writing to the shell
            self.Update() #Calls the update on all child classes
            with cond:
                cond.wait()#This makes all the threads waite(besides the tick thread) when they are done for the next tick.

    def Update(self):
        #This is a place holder.
        #It stops classes that don't have a Update function crashing when called
        pass

    def Unload(self):
        #this method will terminate the thread.... Don't know if this is even possible in python yet
        #and then remove any active instances of itself befor removing the class instance
        pass

    #This class is made so that I did not have to rewrite self.lock.blablabla eatch time i wanted to print to the shell
    def Print (self, message):
        self.lock.acquire()
        print(message)
        self.lock.release()

#---------------------------------------------------------------------------------
class ChildClassA(BaseClass):
    def __init__(self, name):
        super(ChildClassA, self).__init__(name)

    def BaseClassType(self):
        return 'ChildClassA'

    def Update(self):
        self.Print('AAAAAAAAAAAAAAAAAAAAAAA')

#----------------------------------------------------------------------------------
class ChildClassB(BaseClass):
    def __init__(self, name):
        super(ChildClassB, self).__init__(name)

    def Update(self):
        #print(self.name, "This is a completley different action")

        self.Print('BBBBBBBBBBBBBBBBBBB')
        self.Hi()

    def Hi(self):
        self.Print("Hi")
#----------------------------------------------------------------------------------
#works now just like in unity
class ChildClassC(BaseClass): #the new class
    def __init__(self, name): #this is the equivalent of start()
        super(ChildClassC, self).__init__(name) #this is the onley extra bit but its just to pass some name data through the classes

    def Update(self): #this is litaley the same as update() except it has self in the parameters

        self.Print("CCCCCCCCCCCCCCCCCCCCCCCCCC")

#--------------------------------------------------------------------------------

obj1 = ChildClassA('Obj1') 
obj2 = ChildClassB('Obj2')
obj3 = ChildClassC('Obj3')

i=0
d=False
while True:
    if i >=10:
        if d==False:
            del obj1
            d = True
    i=i+1
    print("D: ", d)
    print("I: ", i)

Upvotes: 0

Views: 1480

Answers (1)

Blake O'Hare
Blake O'Hare

Reputation: 1880

It's not clear what you are asking. But I am guessing it is one of the following:

  • You are from a C background where objects are allocated and freed and Python is your first Garbage Collected language. If you remove all references to it (e.g. remove it from the list of sprites or whatnot). Garbage collection will automatically remove it from memory.
  • If you are asking how it is literally removed from a game scene, you will have to be more specific as to what game framework you are using or other existing code.

Upvotes: 1

Related Questions