繁体   English   中英

如何在python中卸载类的实例

[英]How do I unload an instance of a class in python

我认为最好举个例子说明为什么我要这样做。 假设我有一个游戏。 有一些敌人是这些敌人,这些敌人正在运行一个职业(生命,武器等)。如果玩家杀死了一个敌人,我想从游戏中卸载该敌人的数据(移除该职业的敌人实例)去做这个?

编辑:

确定使用del没有帮助。 这是一个基本示例,想象obj1是一个敌人,并且在i = 10的想象力之后该敌人死亡。 我有删除obj的代码,但是仍调用obj1中的功能Update(),因为即使外壳程序将d打印为True,外壳程序仍会打印“ AAAAAAAAAAAA”

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)

不清楚你在问什么。 但是我猜这是以下之一:

  • 您来自C的背景下,在那儿分配和释放对象,而Python是您的第一个垃圾收集语言。 如果删除所有对其的引用(例如,将其从Sprite列表或其他列表中删除)。 垃圾收集将自动将其从内存中删除。
  • 如果您要问如何从游戏场景中真正删除它,您将必须更具体地说明您使用的是什么游戏框架或其他现有代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM