help with looking and gripping tutorial[solved]

well, hello guys, still trying to understand the aswers I received in my last problem, but I have another problem, this time related to the looking and gripping panda3d tutorial.

I´m interested in using the looking feature for now, so I tried to implement it in my game code. I have a class named avatar, shown below(just key stuff):

#Player
class Avatar(DirectObject):
    def __init__(self):
        self.loadActor()        
        taskMgr.add(self.turnHead, "turnHead")

def loadActor(self):
        self.player = render.attachNewNode("player")
        self.playerActor = Actor.Actor("models/player")
        self.playerActor.setScale(0.1,0.1,0.1)
        self.playerActor.reparentTo(self.player)
        self.playerhead = self.playerActor.controlJoint(None, 'modelRoot', 'head')
        self.playerActor.loop("still")

#A simple function to make sure a value is in a given range, -1 to 1 by default
    def restrain(i, mn = -1, mx = 1): return min(max(i, mn), mx)            

    def turnHead(self, task):
        #Check to make sure the mouse is readable
        if base.mouseWatcherNode.hasMouse():
            #get the mouse position as a Vec2. The values for each axis are from -1 to
            #1. The top-left is (-1,-1), the bottom right is (1,1)
            mpos = base.mouseWatcherNode.getMouse()
            #Here we multiply the values to get the amount of degrees to turn
            #Restrain is used to make sure the values returned by getMouse are in the
            #valid range. If this particular model were to turn more than this,
            #significant tearing would be visable
            self.playerhead.setP(restrain(mpos.getX()) * 50)
            self.playerhead.setH(restrain(mpos.getY()) * 20)

        return Task.cont                        #Task continues infinitely

but I´m doing something wrong between def restrain and def turnhead, since I´m still beginning in python/classes/panda3d. I receive this error:

self.playerhead.setP(restrain(mpos.getX()) * 50)
NameError: global name ‘restrain’ is not defined

How do I fix it? Thanks in advance

Didnt read the code u posted but the error means that you didnt defined ‘restrain’ or aka (restrain = 0) or w/e it needs to be.

restrain() looks to be a method as you’re using it. Check wherever you got the syntax you’re using from and see if they defined it somewhere, or imported anything you didn’t. If you got it directly from the API reference, make sure it’s intended to be used to return a coordinate value, not (e.g.) to set up a background task that constantly restrains the passed-in value.

Maybe this is what your after?

#A simple function to make sure a value is in a given range, -1 to 1 by default
def restrain(i, mn = -1, mx = 1): return min(max(i, mn), mx)

#Macro-like function used to reduce the amount to code needed to create the
#on screen instructions
font = loader.loadFont("cmss12")

the code you put above is already included in my program adr, except the font stuff, since I won´t use it yet.

Lonekiltedninja, strangely I sucefully used the looking techincic show in the panda3d example without classes. but when I started to use them(classes) for organization, things have gone out of control.

inside def turnhead(show above), I couldn´t make the program understand which “restrain” is the def written just above the turnhead def.

What do you mean? Thanks in advance

well, managed to fix it. The problem was I forgot to put “self” when i used the restrain def in a calculation, so, the program did not know where to search for the restrain def, so, using self, I just said it should search in the same class both turnhead def and restrain def where.

So, it would be:

#A simple function to make sure a value is in a given range, -1 to 1 by default
    def restrain(i, mn = -1, mx = 1): return min(max(i, mn), mx)           

    def turnHead(self, task):
        #Check to make sure the mouse is readable
        if base.mouseWatcherNode.hasMouse():
            #get the mouse position as a Vec2. The values for each axis are from -1 to
            #1. The top-left is (-1,-1), the bottom right is (1,1)
            mpos = base.mouseWatcherNode.getMouse()
            #Here we multiply the values to get the amount of degrees to turn
            #Restrain is used to make sure the values returned by getMouse are in the
            #valid range. If this particular model were to turn more than this,
            #significant tearing would be visable
            self.playerhead.setP(self.restrain(mpos.getX()) * 50)
            self.playerhead.setH(self.restrain(mpos.getY()) * 20)

        return Task.cont                        #Task continues infinitely

Thanks for those which spent a little of their time to help me