I’m trying to write a very simple method to switch between a main menu and the game world.
The Game is a DirectObject and MainMenu is a DirectObject. I only want one of them to be in memory at a time. The documentation says that to get a DirectObject scheduled for garbage collection to call DirectObject.IgnoreAll(). However, I wanted to double check to see if it was working and as far as I can tell MainMenu is not being garbage collected.
I’m using weakref to check garbage collection, here’s the code in three modules:
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from game import Game
from mainMenu import MainMenu
import weakref
try:
import psyco
psyco.full()
except:
pass
#TODO: Check if the instances are getting garbaged collected.
class SceneChooser(DirectObject):
def __init__(self):
self.accept('startMainMenu', self.startMainMenu)
self.accept('startGame', self.startGame)
self.tester = []
def startMainMenu(self):
self.active = MainMenu()
self.tester.append(weakref.ref(self.active))
def startGame(self):
self.active = Game()
print "in SceneChooser::startGame"
print [instance() for instance in self.tester]
SceneChooser()
messenger.send('startMainMenu')
run()
from direct.showbase.DirectObject import DirectObject
class Game(DirectObject):
def __init__(self):
print "Impliment the game; No problem!"
self.accept('escape', self.gameMenu)
def gameMenu(self):
#TODO: implement a game menu, sending to main menu for now
self.ignoreAll()
messenger.send('startMainMenu')
import sys
from direct.showbase.DirectObject import DirectObject
from direct.gui.OnscreenText import OnscreenText
from direct.gui.DirectButton import DirectButton
from pandac.PandaModules import Vec3,Vec4
from game import Game
class MainMenu(DirectObject):
def __init__(self):
font = loader.loadFont("cmss12")
self.title = OnscreenText(text = "Main Menu", font = font, style = 1, fg = (1,1,1,1),
pos = (0.0,0.5), scale = 0.5)
base.disableMouse()
self.gameButton = DirectButton(pos = Vec3(0,0,0), text = "Start Game",
scale = .1, pad = (.5, .5), text_font = font,
command = self.runGame)
self.exitButton = DirectButton(pos = Vec3(-0.001,0,-0.3), text = "Exit Game",
scale = .1, pad = (0.745,0.5), text_font = font,
command = sys.exit)
def runGame(self):
self.gameButton.remove()
self.exitButton.remove()
self.title.remove()
self.ignoreAll()
messenger.send('startGame')
As far as I can tell I am correctly calling the ignoreAll() to remove the reference that the Panda3D engine has to them and I am not creating any new references in my own code that I can see.
Thanks for your help!