Having a strange problem, name 'self' is not defined

Hi everyone,

I’m pretty much a newbie when it comes to python and panda. I’m having an issue with some code, where I get this message:

“name ‘self’ is not defined” for some reason.

Then I have a simplified test case which I believe is doing the same thing, but I don’t get this message there.

This is the code where I get the error:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.interval.LerpInterval import LerpPosInterval
from direct.interval.SoundInterval import SoundInterval

from pandac.PandaModules import OdeBody, OdeMass, OdeBoxGeom, OdePlaneGeom
from pandac.PandaModules import BitMask32, CardMaker, Vec4, Quat

from OdeFunctions import *

class World(DirectObject, ODEsim):
	def __init__(self):
		self.initODE()
		
		base.setFrameRateMeter(1)
		base.camLens.setNearFar(.1,10000.)
		base.setBackgroundColor(0,0,0,1)
		base.trackball.node().setForwardScale(.005)
		base.camera.setPos(-1,-1,0)
		base.camera.setH(-20)

        # list for ODE simulation objects
		self.simObjects = []

		if self.ode_SPACE.getNumGeoms()>1:
			for o in self.simObjects:
				o.destroy()
			self.simObjects = []
			for j in self.simJoints:
				j = None
	
		self.table = self.createPoolTable(render)
		self.table.setPos(0,5,0)
	
		self.createLights()
		
        self.mainLoop = taskMgr.add(self.gameLoop, "gameLoop")
        self.mainLoop.last = 0	
	
	def gameLoop(self,task):
		dt = task.time - task.last
		task.last = task.time
		if dt>.2 or dt==.0:
			return Task.cont
		self.simulate()
		return Task.cont

... cut ...

World()
run()


The error occurs in this line: self.mainLoop = taskMgr.add(self.gameLoop, “gameLoop”)

Then I simplified the code and removed everything except for this:

This works fine … even though the line that fails in the previous code is here as well, unchanged.

Could anyone help? If you need the complete code in the example which fails, just let me know. I just pasted what I thought was relevant.

Thanks in advance!

There seems to be an extra indent to these lines:

        self.mainLoop = taskMgr.add(self.gameLoop, "gameLoop") 
        self.mainLoop.last = 0 

But I’m not sure if that’s the cause of your problem.

Wow this is weird. I double checked these lines when you mentioned this, and they were indented with spaces, not tabs. I removed the spaces and indented them with actual tabs and now my code runs. Weird, since usually when I screw the indention up, python really lets me hear it :laughing:

Thanks for point me in the right direction though! :wink: