0

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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
skyzzle
  • 167
  • 1
  • 4
  • 16

1 Answers1

1

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.
Blake O'Hare
  • 1,863
  • 12
  • 16
  • Not a C background. The closest I have done to C is C# in unity. I have tryed removing the object using del however the object still runs – skyzzle Sep 01 '16 at 22:14
  • @SkylineGodzilla: Yeah, `del` isn't what you want at all. – user2357112 Sep 01 '16 at 22:24
  • So what do I need? – skyzzle Sep 01 '16 at 22:27
  • @SkylineGodzilla: It depends on how your game is designed. Looking at the code you've posted, it looks like you've used a separate thread to update each object, which turns out to be a pretty terrible way to do things. Anyway, with this design, you'd need to build in a way to tell an object's `UpdateHandler` method to shut down, and then use that. – user2357112 Sep 01 '16 at 23:18
  • @user2357112 Yeah you were right I cant beleave i missed it All i needed to do was call a method to end the Loop thats in the updatehandler methed. It lets the method finish and the thread close. However now Im curious about what you said about "you've used a separate thread to update each object, which turns out to be a pretty terrible way to do " As apposed to what? It was the only way I could think of that allows each object to update itself. I am open to suggestions of better ways to do that. – skyzzle Sep 02 '16 at 01:54
  • @SkylineGodzilla: You can just have one thread handle the updates. It's a lot easier to maintain consistency that way. You might feel like doing things that way means that the objects aren't "updating themselves" any more, but giving each object its own updater thread turns out not to have any major advantages. – user2357112 Sep 02 '16 at 02:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122463/discussion-between-skylinegodzilla-and-user2357112). – skyzzle Sep 02 '16 at 02:08