a bit trouble with a task

Hello, i have a camera in a corner of an inner, just want to make the camera looks the player moving around. A task should be a solution right?

i have this :

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task import Task
from direct.showbase.DirectObject import DirectObject


class Stage_cero(DirectObject):

	def __init__(self):
	
		base.camLens.setFov(85)
		base.disableMouse()
		base.camera.setPos(206, 172, 131)
		
		
		self.stage0 = loader.loadModel("../models/stage0")
		self.stage0.reparentTo(render)
		self.stage0.setScale(10)
		
		self.panda = loader.loadModel("models/panda-model")
		self.panda.reparentTo(self.stage0)
		self.panda.setScale(0.03)
		
		self.accept("q", self._getCameraPos)
		self.accept("a", self._moveLeft)
		self.accept("d", self._moveRight)
		
	
	def _moveLeft(self):
		self.panda.setPos(self.panda.getX() + 10, self.panda.getY(), self.panda.getZ())
	
		
	def _moveRight(self):
		self.panda.setPos(self.panda.getX() - 10, self.panda.getY(), self.panda.getZ())
		

	def _getCameraPos(self):
		print base.camera.getPos()
		
	
	def _camFollowChar(task,self):
		base.camera.lookAt(self.panda)
		return task.cont
	
	taskMgr.add(self._camFollowChar, "CameraFollowsChar")

but when i try to execute, i got this error “File “C:\Skykobe\ProjectA\data\stage0.py”, line 45, in Stage_cero
taskMgr.add(self._camFollowChar, “CameraFollowsChar”)
NameError: name ‘self’ is not defined”, evidently i dont know why system tells my that self is not defined if i have it in the parameter of the function.

Hi there

You need to add that task to the task manager during the initialisation of the class instance. (Someone cleverer than me will have to explain why it doesn’t work where it is… I am a complete newbie to all this)

(You also need to add an instance of the class and run the game, but I suppose you are doing that in another module.)

Oh, and arguments to the “camFollowChar” method are in the wrong order. “self” always comes first.

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task import Task
from direct.showbase.DirectObject import DirectObject


class Stage_cero(DirectObject):

   def __init__(self):
   
      base.camLens.setFov(85)
      base.disableMouse()
      base.camera.setPos(206, 172, 131)
      
      
      self.stage0 = loader.loadModel("../models/stage0")
      self.stage0.reparentTo(render)
      self.stage0.setScale(10)
      
      self.panda = loader.loadModel("models/panda-model")
      self.panda.reparentTo(self.stage0)
      self.panda.setScale(0.03)
      
      self.accept("q", self._getCameraPos)
      self.accept("a", self._moveLeft)
      self.accept("d", self._moveRight)
      taskMgr.add(self._camFollowChar, "CameraFollowsChar") 
      
   
   def _moveLeft(self):
      self.panda.setPos(self.panda.getX() + 10, self.panda.getY(), self.panda.getZ())
   
      
   def _moveRight(self):
      self.panda.setPos(self.panda.getX() - 10, self.panda.getY(), self.panda.getZ())
      

   def _getCameraPos(self):
      print base.camera.getPos()
      
   
   def _camFollowChar(self,task):
      base.camera.lookAt(self.panda)
      return task.cont
   
s = Stage_cero()
run()

Its work Perfect!, thank you very much … :wink:

In Digimikeh’s original code, the taskMgr.add is sitting in class attribute space. (Or whatever Python calls it.) Since this is before the class is instanced, there is no “self” to speak of as of yet. That’s why moving it to the constructor works.

Note that Python really plays loose and fast with the concept of object-oriented, so I’m mostly speaking from a C+±like manner. “Constructor” and “Attributes” are just conveniences.

yes, i guess, i trying now to separate into modules everything.