Main menu problem

Hi guys.

I created a small game and it’s quite finished. In order for it to look a bit more like a game, I created a small menu which starts the game. I have a big World class which contains my game and a GUI class which uses directGUI for the menu. For now, I only have 2 directButtons, one for start and one for exit. I also made a task manager function which checks if the user pressed escape to exit the game. If that’s the case, the task manager destroys the World instance, and creats the two buttons yet again, as if the player returns to the main menu.

However, when I start the game again it appears as some of the graphics were left and the framerate really suffers. It’s as if the former instance wasn’t fully destroyed (although by the analyze function, it was).

Am I missing something here? Anyone with a better idea?

I have another problem in which the exit button function has the sys.exit function yet it doesn’t exit.

I should note that the game runs fine on its own (without the GUI menu)

Thanks in advance guys. :slight_smile:

Make sure your world class destroys everything it had.
Once I had the same problem, and resolved it designing carefully the hierarchy in which my objects were creating and deleting instances.
You can keep a reference to every model loaded and call removeNode on everyone, or try a brutal approach like

render.removeChildren()
render.setLightOff()
base.camera.reparentTo(render)

before re-creating your starting menu.
Please note that the second approach is really naive, use it just to see if missing deletes was really the problem.

Well, I just checked and yes I’m destroying everything. I used your approach just in case I missed something and when I run my game for the 1st time, the framrate is locked at 60, in the 2nd time it in the 50s, in the 3rd time it drops to the 20-40 and then lower than that.

Here’s the code of my simple GUI. Maybe I’m doing something wrong there…

class MyGUI:
	def __init__(self):

		self.w = 0
		taskMgr.add(self.updateMenu,"Menu") 
		#self.a = DirectFrame(relief = 1, scale = 1, text = 'Sasi The \n Cat', text_scale = 0.6, frameTexture = 'world_textures/tree1.jpg')
		#self.a.setZ(self.a.getZ() - 0.5)
		self.newGame = DirectButton(text = ("New Game", "Loading...", "New Game"), scale = 0.1, command = self.startGame)
		self.newGame.setZ(self.newGame.getZ() + 0.4)

		self.exitGame = DirectButton(text = ("Exit Game", "Exit Game","Exit Game"), scale = 0.1, command = self.endGame)
		self.exitGame.setZ(self.newGame.getZ() - 0.6)
				
	def startGame(self):
		self.w = mainWorld.World()
		self.newGame.hide()
		self.exitGame.hide()
		
	def endGame(self):
		sys.exit
		print('exit')
		
	def updateMenu(self, task):
		if (self.w != 0 and self.w.getExitGame() is True):
			self.w.__del__()
			self.w = 0 
			self.newGame = DirectButton(text = ("New Game", "Loading...", "New Game"), scale = 0.1, command = self.startGame)
			self.newGame.setZ(self.newGame.getZ() + 0.4)

			self.exitGame = DirectButton(text = ("Exit Game", "Exit Game","Exit Game"), scale = 0.1, command = self.endGame)
			self.exitGame.setZ(self.newGame.getZ() - 0.6)
		return Task.cont
		
gui = MyGUI()

run()

PS - I know I’m creating new instances of buttons. I tried destroying them and I also tried showing the hidden once. Didn’t help.

Hmm…anyone has an idea? I’m quite stuck.

Does it do this after you click the DirectButton New Game?

You can try to .removeNode() your Button. As for the UpdateMenu that may be your problem. But I cant see all your code so not sure. If both thos become true and keep true they would cause thos Button to be copy over and over again.

Also sys.exit needs to be sys.exit()

Other then that idk. code seems to be ok from what I can do with it.

You may need to isolate if the problem is on gui side or on your world object. If the world cleanup is not completed, next time you create the world object may cause significant performance issues.

The problem is in the GUI side.

I know this because I clean everything in the World class destructor, and I used render.analyze() to confirm it.

Guys, I think I solved it.

I did clean everything, the problem is I clean everything from the render and the lights and I forgot to use the cleanup() function for the particle effects, so those stayed on and I couldn’t notice it visually but the frame rate did notice it.

Thanks a lot for your help guys :slight_smile: