Class Objects and Tasks

hey all

i’m trying to make the submarine in my game fire torpedoes so in the submarine class i have done this :

def SetFire(self, key):
	self.accept(key, self.Fire)
	
def Fire(self):

	tempX = self.submarine.getX()
	tempY = self.submarine.getY()
	tempZ = self.submarine.getZ()
	if (self.greenTorpCount == 1):
		Torpedo(tempX, tempY, tempZ)
		self.greenTorpCount = 0

then the torpedo class as it is now has :

class Torpedo(DirectObject):
def init(self, x, y, z):
travelDistance = 0
maxTravelDistance = 0
vel = 0
origVel = 0
accFactor =0.8

	self = loader.loadModel("models/torpedoGreen")  
	self.setPos(x,y,z)
	self.reparentTo(render)
	self.setScale(0.05) 
	self.setHpr(90,0,0)
	taskMgr.add(self.MoveForwardTask,'MoveForwardTask')
	
	
	
def MoveForwardTask(self, task):

and then inside the MoveForwardTask is where i put the movement for the torpedo. the problem is when i run it, it says “cannot find global name MoveForwardTask” eventho it is written in the torpedo class directly under the taskMgr.add statement!

can someone plz correct me somewhere :smiley:

thanks as always guys

I may be wrong, but shouldn’t the task be above that statement to be found? That is, the interpreter must have ‘seen’ the task before it can be referenced?

You may be able to woraround this if you first create the task using

theMoveForwardTask = Task.Task(self.moveForwardTask)

before calling

taskMgr.add(theMoveForwardTask, "moveForwardTask")

(I added ‘the’ to make explicit the difference between the instance and the definition of this task)

I suspect your original code will work as well if you swap the last two def’s

I believe that ‘Cannot find global name’ suggests you may have a tabbing typo somewhere. Python won’t try to resolve self.MoveForwardTask in the global name scope; it’ll try to resolve it as a member of self. So I’d check two things

  1. Are you sure the taskMgr.add line is the one that is bombing?
  2. Are you sure that both the line that is bombing and the definition of MoveForwardTask are tabbed into the right scope?

Best of luck!
-Mark

Your post is awful to read, you should use the code statement…

Is the moveforward task inside the torpedo class?

As I said, I can’t tell what the structure of your code is…(you know, you can’t read python code correctly without tabs and spaces)
but I guess your movement task is in the same class as all the other things (like def Fire…), let’s suggest it’s the main class…

Now your Torpedo class tries to call the movement task, and that just can’t work with self.

Because the movement task is not inside your torpedo class…

You’ll have to write something else.
Let’s assume your code is more like this:

class main(DirectObject.DirectObject):

    def SetFire(self, key):
        self.accept(key, self.Fire)

    def Fire(self):

        tempX = self.submarine.getX()
        tempY = self.submarine.getY()
        tempZ = self.submarine.getZ()
        if (self.greenTorpCount == 1):
            Torpedo(tempX, tempY, tempZ)
            self.greenTorpCount = 0

    def MoveForwardTask(self, task): 



class Torpedo(DirectObject):
    def __init__(self, x, y, z):
        travelDistance = 0
        maxTravelDistance = 0
        vel = 0
        origVel = 0
        accFactor =0.8


        self = loader.loadModel("models/torpedoGreen")
        self.setPos(x,y,z)
        self.reparentTo(render)
        self.setScale(0.05)
        self.setHpr(90,0,0)
        taskMgr.add(self.MoveForwardTask,'MoveForwardTask')


start = main()

run()

Well, now that I’ve structured your code I can see at least another mistake… How the hell did you come to the Idea to call one of your models self ???
that will of course never work, because the taskMgr now thinks that self.MoveForwardTask is a part of your self model… and it isn’t

you should call your model something lieke self.torpedo and then try again…

Anyway, back to the first problem of the movement task being in another class then the taskMgr.add…
In the above example you would have to type this:

taskMgr.add(start.MoveForwardTask,'MoveForwardTask')

I suggest you read some example code, that’ll help you learn more about panda and python, and it often answers questions you didn’t have before :wink: