Speed self changing problem

As shown in the following program, why is my “self. Search_speed” changed during the movement? Isn’t this a process of assignment?

    self.search_speed = Vec3(0, 50, 0)
    self.varyangle = Vec2(0, 1)

def update(self,player,dt):
        ActorObject.move(self, dt)
        position_vevtor = player.actor.getPos() - self.actor.getPos()
        position_vevtor2D = position_vevtor.getXy()
        distanceToplayer = position_vevtor2D.length()
        position_vevtor2D.normalize()
        #print('wood',self.health)
        self.actor.enableBlend()
        if self.changeQueue.getNumEntries() > 0 and self.change:
                self.search_speed = -self.search_speed
                self.varyangle = -self.varyangle
                self.change = False
                self.attackWaitTimer = 0.05
        else:
            self.attackWaitTimer -= dt
            if self.attackWaitTimer <= 0:
                self.change = True
        if distanceToplayer > self.detectiondistance:
            self.walking = True
            self.heading = self.standard.signedAngleDeg(self.varyangle)
            print(self.heading,self.varyangle)
            self.velocity = self.search_speed
            print(self.velocity,self.search_speed)
            self.actor.setControlEffect('walk', 0.7)
            self.actor.setControlEffect('attack', 0.3)
            
        elif distanceToplayer < self.detectiondistance and distanceToplayer > 30:
            self.heading = self.standard.signedAngleDeg(position_vevtor2D)
            self.maxspeed = 50
            self.walking  = True
            self.actor.setPlayRate(2, 'walk')
            self.velocity += position_vevtor * self.acceleration * dt
            self.actor.setControlEffect('walk', 0.9)
            self.actor.setControlEffect('attack', 0.1)
            print(111,distanceToplayer)
        elif self.attackWaitTimer > 0 and distanceToplayer <= 30:
            self.actor.disableBlend()
            self.heading = self.standard.signedAngleDeg(position_vevtor2D)
            self.walking = False
            print(222, distanceToplayer)
            self.actor.setPlayRate(0.3, 'attack')
            self.attackWaitTimer -= dt
            if self.attackWaitTimer <= 0  :
                self.attackWaitTimer = random.uniform(0.5, 0.7)
                self.actor.play('attack')
                player.alterHealth(self.Hue)
        if self.walking:
            walkingControl = self.actor.getAnimControl("walk")
            if not walkingControl.isPlaying():
                self.actor.loop("walk")
                self.actor.loop("attack")
        self.actor.setH(self.heading + 180)

I find out if the vector assignment in Panda3D always follows the shallow “copy” rule, and there will always be some associated modifications. This really bothered me several times.

1 Like

Yup: I think that in Python, non-base types (that is, things like Vectors, as opposed to simple ints or floats) are generally passed by reference rather than by value.

You might be able to get around this by either using the “set” method of the “Vector” class to copy the components of your “search_speed” vector into the other vector, or you could perhaps use copy-construction.

Using the “set” method should look something like this:

self.velocity.set(self.search_speed.x, self.search_speed.y, self.search_speed.z)

Mm-hmm. you’re right. It’s really solved! Thanks again!

1 Like

It’s my pleasure! I’m glad that this and the problems from two of your other threads have been solved! :slight_smile: