Object.__init__() takes exactly one argument (the instance to initialize)

Hi,

I am new to python and Panda3D. I am following along with Panda3Ds beginner lessons and Iv come across an issue as the title says. I am in need of assistance to solve this issue that I can’t seem to find a solution for.

I am working in Visual Studio Code as my IDE of choice if that matters. So with that being said the program is telling me that init() can only take one argument.

Exception has occurred: TypeError
object.init() takes exactly one argument (the instance to initialize)
File “D:\Visual Studio Code\GameObject.py”, line 77, in init
“player”)
File “D:\Visual Studio Code\Game.py”, line 102, in init
self.player = Player()
File “D:\Visual Studio Code\Game.py”, line 130, in
game = Game()

from direct.showbase.ShowBase import ShowBase
from direct.actor.Actor import Actor
from panda3d.core import CollisionTraverser, CollisionHandlerPusher, CollisionSphere, CollisionTube, CollisionNode
from panda3d.core import AmbientLight, DirectionalLight
from panda3d.core import Vec4, Vec3
from panda3d.core import WindowProperties
from panda3d.core import CollisionTube


from GameObject import *

class Game(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        self.disableMouse()

        properties = WindowProperties()
        properties.setSize(1000, 750)
        self.win.requestProperties(properties)

        mainLight = DirectionalLight("main light")
        self.mainLightNodePath = render.attachNewNode(mainLight)
        self.mainLightNodePath.setHpr(45, -45, 0)
        render.setLight(self.mainLightNodePath)

        ambientLight = AmbientLight("ambient light")
        ambientLight.setColor(Vec4(0.2, 0.2, 0.2, 1))
        self.ambientLightNodePath = render.attachNewNode(ambientLight)
        render.setLight(self.ambientLightNodePath)

        render.setShaderAuto()

        self.environment = loader.loadModel("Models/Misc/environment")
        self.environment.reparentTo(render)

        self.tempActor = Actor("Models/PandaChan/act_p3d_chan", {"walk" : "Models/PandaChan/a_p3d_chan_run"})
        self.tempActor.getChild(0).setH(180)
        self.tempActor.reparentTo(render)
        self.tempActor.loop("walk")

        self.camera.setPos(0, 0, 32)
        self.camera.setP(-90)

        self.keyMap = {
            "up" : False,
            "down" : False,
            "left" : False,
            "right" : False,
            "shoot" : False
        }

        self.accept("w", self.updateKeyMap, ["up", True])
        self.accept("w-up", self.updateKeyMap, ["up", False])
        self.accept("s", self.updateKeyMap, ["down", True])
        self.accept("s-up", self.updateKeyMap, ["down", False])
        self.accept("a", self.updateKeyMap, ["left", True])
        self.accept("a-up", self.updateKeyMap, ["left", False])
        self.accept("d", self.updateKeyMap, ["right", True])
        self.accept("d-up", self.updateKeyMap, ["right", False])
        self.accept("mouse1", self.updateKeyMap, ["shoot", True])
        self.accept("mouse1-up", self.updateKeyMap, ["shoot", False])

        self.pusher = CollisionHandlerPusher()
        self.cTrav = CollisionTraverser()

        self.pusher.setHorizontal(True)

        colliderNode = CollisionNode("player")
        colliderNode.addSolid(CollisionSphere(0, 0, 0, 0.3))
        collider = self.tempActor.attachNewNode(colliderNode)

        base.pusher.addCollider(collider, self.tempActor)
        base.cTrav.addCollider(collider, self.pusher)

        wallSolid = CollisionTube(-8.0, 0, 0, 8.0, 0, 0, 0.2)
        wallNode = CollisionNode("wall")
        wallNode.addSolid(wallSolid)
        wall = render.attachNewNode(wallNode)
        wall.setY(8.0)

        wallSolid = CollisionTube(-8.0, 0, 0, 8.0, 0, 0, 0.2)
        wallNode = CollisionNode("wall")
        wallNode.addSolid(wallSolid)
        wall = render.attachNewNode(wallNode)
        wall.setY(-8.0)

        wallSolid = CollisionTube(0, -8.0, 0, 0, 8.0, 0, 0.2)
        wallNode = CollisionNode("wall")
        wallNode.addSolid(wallSolid)
        wall = render.attachNewNode(wallNode)
        wall.setX(8.0)

        wallSolid = CollisionTube(0, -8.0, 0, 0, 8.0, 0, 0.2)
        wallNode = CollisionNode("wall")
        wallNode.addSolid(wallSolid)
        wall = render.attachNewNode(wallNode)
        wall.setX(-8.0)

        self.updateTask = taskMgr.add(self.update, "update")
        
        self.player = Player()
        
        self.tempEnemy = WalkingEnemy(Vec3(5, 0, 0))

    def updateKeyMap(self, controlName, controlState):
        self.keyMap[controlName] = controlState

        self.player.update(self.keyMap,dt)
        
        self.tempEnemy.update(self.player, dt)

    def update(self, task):
        dt = globalClock.getDt()

        if self.keyMap["up"]:
            self.tempActor.setPos(self.tempActor.getPos() + Vec3(0, 5.0*dt, 0))
        if self.keyMap["down"]:
            self.tempActor.setPos(self.tempActor.getPos() + Vec3(0, -5.0*dt, 0))
        if self.keyMap["left"]:
            self.tempActor.setPos(self.tempActor.getPos() + Vec3(-5.0*dt, 0, 0))
        if self.keyMap["right"]:
            self.tempActor.setPos(self.tempActor.getPos() + Vec3(5.0*dt, 0, 0))
        if self.keyMap["shoot"]:
            print ("Zap!")

        return task.cont


game = Game()
game.run()
from panda3d.core import Vec3, Vec2
from direct.actor.Actor import Actor
from panda3d.core import CollisionSphere, CollisionNode 

FRICTION = 150.0

class GameObject():
    def _init__(self,pos, modelName, modelAnims, maxHealth, maxSpeed, colliderName):
        self.actor = Actor(modelName, modelAnims)
        self.actor.reparentTo(render)   
        self.actor.setPos(pos)

        self.maxHealth = maxHealth
        self.health = maxHealth

        self.maxSpeed = maxSpeed

        self.velocity = Vac3(0,0, 0)
        self.acceleration = 300.0

        self.walking = False

        colliderNode = CollisionNode(colliderName)
        colliderNode.addSolid(CollisionSphere(0, 0, 0, 0.3))
        self.collider = self.actor.attatchNewNode(colliderNode)
        self.collider.setPythonTag("owner", self)

def update(self, dt):
    speed = self.velocity.length()
    if speed > self.maxSpeed:
       self.velocity.normalize()
       self.velocity *= self.maxSpeed
       speed = self.maxSpeed

    if not self.walking:
        frictionVal = FRICTION*dt
        if frictionVal > speed: 
            self.velocity.set(0, 0, 0)     
        else:
            fricitonVac = -self.velocity
            frictionVac.normalize()
            frictionVac *= frictionVal

            self.velocity += frictionVac

    self.factor.setPos(self.actor.getPos() + self.velocity*dt)

def alterHealth(self, dHealth):
    self.health += dhealth

    if self.health > self.maxHealth:
        self.health = self.maxHealth

def cleanup(self):
    if self.collider is not None and not self.collider.isEmpty():
       self.collider.cleanPythonTag("owner")
       base.cTrav.removeCollider(self.collider)
       base.pusher.removeCollider(self.collider)

    if self.actor is not None:
       self.actor.removeNode()
       self.actor = None

    self.collider = None     

class Player(GameObject):
    def __init__(self):
        GameObject.__init__(self,
                            Vec3(0, 0, 0),
                            "Models/PandaChan/act_p3d_chan",
                              {
                                  "stand" : "Models/PandaChan/a_p3d_chan_idle",
                                  "walk" : "Models/PandaChan/a_p3d_chan_run"
                              },
                            5,
                            10,
                            "player")
        self.actor.getChild(0).setH(180)
    
        base.pusher.addCollider(self.collider, self.actor)
        base.cTrav.addCollider(self.collider, base.pusher)

        self.actor.loop("stand")

    def update(self, keys, dt):
        GameObject.update(self, dt)

        self.walking = False

        if keys["up"]:
            self.walking = True
            self.velocity.addY(self.acceleration*dt)
        if keys ["down"]:
            self.walking = True
            self.velocity.addY(-self.acceleration*dt)
        if keys ["left"]:
            self.wallking = True
            self.volocity.addX(self.acceleration*dt)
        if keys["right"]:
            self.walking = True
            self.velocity.addX(self.accleration*dt)     

        if self.walking:
            standContol = self.actor.getAnimControl("stand")
            if standControl.isPlaying():
                standControl.stop()

            walkControl = self.actor.getAnimControl("walk")
            if not walkControl.isPlaying():
                standControl. stop()
        else:         
           walkControl = self.getAnimControl("stand")
           if not walkControl.isPlaying():
               self.actor.loop("walk")
               self.actor.loop("stand")   

class Enemy(GameObject):
    def __init__(self, pos, modelAnims, maxHeath, maxSpeed, colliderName):
        GameObject.__init__(self, pos, modelName, modelAnims, maxHealth, maxspeed, colliderName)

        self.socreValue = 1 

    def update(self, player, dt):
            GameObject.update(self, dt)

            self.runLogic(player, dt)

            if self.walking:
                walkingControl = self.actor.getAnimControl("walk")
                if not walkingControl. isPlaying():
                    self.actor.loop("walk")

            else: 
                spawmControl = self.actor.getAnimControl("spawn")
                if spawnControl is None or not spawnControl.isPlaying():
                    attackControl = self.actor.getAnimControl("attack")
                    if attackControl is None or not attackControl.isPlaying():
                        standControl = self.actor.getAnimControl("stand")
                        if not standConto.isPlaying():
                             self.actor.loop("stand")

def runLogic(self, player, dt):
    pass

class WalkingEnemy(Enemy): 
    def _init__(self, pos):
        Enemy._init__(self, pos,
                      "Models/Misc/simpleEnemy",
                      {
                      "stand" : "Models/Misc/simpleEnemy-stand",
                      "walk" : "Models/Misc/simpleEnemy-walk",
                      "attack" : "Models/Misc/simpleEnemy-attack",
                      "die" : "Models/Misc/simpleEnemy-die",
                       "spawn" : "Models/Misc/simpleEnemy-spawn"
                        },
                        3.0,
                        7.0,
                        "walkingEnemy")

        self.attackDistance = 0.45

        self.acceleration = 100.0

        self.yVector = Vec2(0, 1) 

    def runLogic(self, player, dt):
        vectorToPlayer = player.actor.getpos() - self.actor.getPos()

        vetorToPlayer2D = vetorTopPlayer.getXy()
        distanceToPlayer = vectorToPlayer2D.length()

        vetorToPlayer2D.normalize()

        heading = self.yVector.signedAngleDeg(vectorToPlayer2D)


        if distanceToPlayer > self.attackDistance*0.9:
            self.walking = True
            vectorTopPlayer.setZ(0)
            vectorToPlayer.normalize()
            self.velocity += vectorToPlayer*self.acceleration*dt
        else:
            self.walking = False
            self.velocity.set(0,0, 0)

        self.actor.setH(heading)                            

On thing that I notice is that, if it’s not a typo in the post, then your “init” method in “GameObject” has only one underscore at the start. That is, you have “_init__” instead of “__init__”.

That said, I’m not familiar with the effect of doing so, and thus I’m I don’t know whether it might cause the error that you’re seeing.

Thank you for pointing that out to me I know I probably missed it so may time while trying to figure out why I am having these errors.

I corrected my mistakes but it did seem to be the cause of the problem. I appropriate you trying to help me.

Not a problem; good luck with the rest of your project! :slight_smile:

1 Like

I just want to say thank you! :slight_smile:
I’ve realized you’re the author of the reference code for the project and for some reason your scripted fixed my issue lol. I still can’t help but wonder why. But whats important i was able to fix it thanks to you!

1 Like

Hahah, it’s my pleasure! I’m glad if you’re finding the tutorial useful. :slight_smile:

As to the error, well, the __init__ method is supposed to have two underscores to either side. I’m really not sure of what Python makes of it if you have only one in either position.

My guess is that the method isn’t recognised as a constructor without the right number of underscores, and thus Python falls back to the constructor belonging to the base built-in “object” class. That constructor, I imagine, has no parameters other than “self”, and thus an attempted construction with other parameters fails, as you saw.

That sounds about right. :sweat_smile:
I’ll be sure to post here again when I need help. :slight_smile:

1 Like