Hidden arguments in class' __init__()

So it’s me again; I’ve been having a load of trouble separating the code for the player character from the rest of the code and putting it into its own class. My earlier attempts were plagued with errors telling me that global name “render” wasn’t defined, or that Actor() didn’t have an instance of “render”, and so on. Now I tried again, from scratch, and I get the following error message:

TypeError: init() takes exactly 1 argument (3 given)

But I’m not giving init() 3 arguments (or 2, as that’s what it would be, wouldn’t it?). All it is is

def __init__(self):

and I call it with

        self.loadActor()
   
    def loadActor(self):
        player = Actor()

So I can’t see where it’s getting all these arguments from.

Note that the last line the error message brings up is neither of these, but in fact the third line of code in the init() method, where I define self.actor; these are the first five lines:

        self.actorNP = NodePath("smiley")
        self.actorNP.reparentTo(base.render)
        self.actor = Actor("PacMan", {"walk":"PacManWalk", "shoot":"PacManShoot"})
        self.actor.reparentTo(self.actorNP)
        self.isWalking = self.actor.getAnimControl("walk")

What’s going on here? Is there something I’m missing, some kind of rule or way of doing things?

Thanks for the help! :slight_smile:

What is Actor? Is it your own class? Isn’t it likely to get confused with the Panda class called Actor (and whose constructor you are calling with two arguments)?

Probably better to name your class something else.

David

Fairly certain Panda’s Actor class doesn’t have an attribute of render. The error you received there is a method/attribute error. You can use dir(Actor) to get the methods and attributes of an object in Python, iirc.

You could write your code as something like this:

class Player(Actor):
    def __init__(self, args):
    Actor.__init__(self, args)
    ...stuff...

Gets back to understanding inheritance in Python.

Ah, well, that was an embarassing mistake. I corrected all that, changing the class name to Player() to avoid the mix-up, and got the usual global name/instance errors. I found out that the problem was that I was creating an instance of Player() within World()'s init() method. I don’t understand this at all, but I did discover that when I bind creating the instance of Player() to a key that has to be pressed, the errors stop, so that’s good.

After that, I fiddled around with various arguments, trying to fix all the error messages I was getting, and eventually, through some trial and error, I managed to get it right! Now I have to clean up the code, which has become very messy. This has been, all things considered, a learning experience on integrating classes into my game. Thanks for the help! :smiley: