How to Change an Animation Speed in BGE

So I’ve been tampering with a script somebody created for my game. So I’ve added features like being wounded and walking slower while aiming in my game. The problem is that I’ve got the movement covered but I’ve been having troubles with the animations…

Here’s a script, I’ve bolded, italicized, and underlined the essential parts…


from bge import logic, events


scene = logic.getCurrentScene()
owner = logic.getCurrentController().owner
armature = scene.objects['PlayerRig']


move = 0
delay = 10
cool = 10


key_none = logic.KX_INPUT_NONE
key_tap = logic.KX_INPUT_JUST_ACTIVATED
key_active = logic.KX_INPUT_ACTIVE


UPARROWKEY = logic.keyboard.events[events.UPARROWKEY]


### Double Tap to run ###


## Tap Counter ##
if UPARROWKEY == key_tap:
owner["TAP"] += 1
if owner["TAP"] >= 2:
owner["TAP"] = 2
owner["TIMER"] = 0
owner["RUN"] = True


## Double Tap Timer ##
if owner["TAP"] == 1:
owner["TIMER"] += 1
if owner["TIMER"] > delay:
owner["TIMER"] = 0
owner["TAP"] = 0


## Move ## (NAME, START, END, LAYER, PRIORITY, BLENDIN, PLAY, LYRWGHT, IPO_FLG, SPEED, BLEND)
BLENDTYPE = logic.KX_ACTION_BLEND_BLEND
PLAYTYPE = logic.KX_ACTION_MODE_LOOP
LYRWGHT = 1
IPO_FLG = 0
<i><i><b>SPEED = 1</b></i></i>
BLEND = 5


<i><i><b>if UPARROWKEY == key_active:</b>
<b> if owner["RUN"] == True:</b>
<b> armature.playAction("PlayerRunning", 1, 9, 0, 0, BLEND, PLAYTYPE, LYRWGHT, IPO_FLG, SPEED, BLENDTYPE)</b>
<b> move = -0.12</b>
<b> else:</b>
<b> armature.playAction("PlayerWalking", 1, 9, 0, 0, BLEND, PLAYTYPE, LYRWGHT, IPO_FLG, SPEED, BLENDTYPE)</b>
<b> move = -0.08</b></i></i>


## Reset Walk Cool Down ##
elif UPARROWKEY == key_none:
armature.playAction("PlayerWalking", 0, 0, 0, 0, BLEND, logic.KX_ACTION_MODE_PLAY, LYRWGHT, IPO_FLG, SPEED, BLENDTYPE)
    
if owner["TAP"] == 2:
owner["TIMER"] -= 1


if owner["TIMER"] &lt; -cool:
owner["TAP"] = 0
owner["TIMER"] = 0
owner["RUN"] = False
    
<i><i><b>##Factors that will change speed##</b>

<b>if owner["Armed_Primary"] == False:</b>
<b> move *= 1.25</b>

<b>if owner["Wounded"] == True:</b>
<b> move *= 0.75</b>
<b> SPEED *= 0.5</b>
</i></i> 

AimingTarget = scene.objects['PlayerCam']


if AimingTarget["Aiming"] == True:
move *= 0.65
if owner["RUN"] == True:
AimingTarget["Aiming"] == False


owner.applyMovement((0, move, 0), True)



CURRENT PROBLEM:
SPEED is part of the armature.playAction(). any changes to the SPEED variable need to happen BEFORE any playAction() is called.

speed should be a game property. currently, the speed variable gets reset each frame. if SPEED was owner[“SPEED”], then that variable would be remembered. then you dont want to have the line SPEED=1 anymore (that would reassign to 1 every frame)

NOTE:
the forums dont like tabs, so be aware that any code you copy and paste will be in spaces likely. you cannot mix tabs and spaces.

using [CODE*] [/CODE*] tags might support tabs if they are pasted in (remove the *).

Thanks, I fixed it.

CURRENT PROBLEM:
SPEED is part of the armature.playAction(). any changes to the SPEED variable need to happen BEFORE any playAction() is called.

speed should be a game property. currently, the speed variable gets reset each frame. if SPEED was owner[“SPEED”], then that variable would be remembered. then you dont want to have the line SPEED=1 anymore (that would reassign to 1 every frame)

Alright, I’ll try doing that…

Okay, so how do I change the animation speed here?

Sorry, I have to scripts that are differently formatted…


import bge


def main():


    cont = bge.logic.getCurrentController()
    owner = cont.owner
    
    keyboard = bge.logic.keyboard
    mouse = bge.logic.mouse
    scene = bge.logic.getCurrentScene()
    armature = scene.objects['PlayerRig']
    
    ## Move (NAME, START, END, LAYER, PRIORITY, BLENDIN, PLAY, LYRWGHT, IPO_FLG, SPEED, BLEND)##
    SPEED = 1
    #Move up down, move left right, arms walking/running animations while unarmed##
    

    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DOWNARROWKEY]:
        owner.applyMovement((0, 0.05, 0), True)
        armature.playAction("PlayerWalking", 9, 1, 1, 0)
        if owner["Armed_Primary"] == False:
            armature.playAction("PlayerWalkingArms", 1, 9, 3, 3)

https://docs.blender.org/api/blender_python_api_current/bge.types.KX_GameObject.html#bge.types.KX_GameObject.playAction

the docs are your friend. make sure all the arguments are filled for playAction(), i think its safer.

condition == event: kind of backwards, but not a real problem.

define your SPEED variable the line before your playAction()

the comment line tells you all the available args for playAction()

The animation won’t play for some reason. I’ve done all I can to figure it out…

*Updated


import bge


def main():


    cont = bge.logic.getCurrentController()
    owner = cont.owner
    
    keyboard = bge.logic.keyboard
    mouse = bge.logic.mouse
    scene = bge.logic.getCurrentScene()
    armature = scene.objects['PlayerRig']
    
    ## Move (NAME, START, END, LAYER, PRIORITY, BLENDIN, PLAY, LYRWGHT, IPO_FLG, SPEED, BLEND)##
    SPEED = 1
    #Move up down, move left right, arms walking/running animations while unarmed##
    
    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DOWNARROWKEY]:
        owner.applyMovement((0, 0.05, 0), True)
        armature.playAction("PlayerWalking", 9, 0, 0, 0, 5, logic.KX_ACTION_MODE_LOOP, 1, 0, 1, logic.KX_ACTION_BLEND_BLEND)
main()



check the console for errors.

dont use “def main():” unless you are running your python brick in module mode.

check your animation priorities and layers. running an animation in LOOP will continue to run the animation, even after the playAction() is not being called.

I’ve tampered with the layers and priorities. I’ve also removed the “def main():”

I’ve also tried logic.KX_ACTION_MODE_PLAY. Which begs the question…

Do I use logic.KX_ACTION_MODE_PLAY or KX_ACTION_MODE_PLAY? That might be a factor…

Nevermind I guess…

I guess you’re right. The docs are my friend, and so are the people who supported me on here…

This worked for some reason…

armature.playAction("PlayerWalking", 9, 1, 1, 1, 5, 1, 0, 0, 1, 0)

logic is a module of bge which contains constant variables such as KX_ACTION_MODE_PLAY and KX_INPUT_ACTIVE which are just glorified integers usually.

bge.logic.KX_ACTION_MODE_PLAY since you are importing bge and all of its modules.

if you “from bge import logic”, then you would only import the logic module from the bge.

then it would be: logic.KX_ACTION_MODE_PLAY

you can also assign an imported module to your own variable using “from bge import logic as lgc”. however, this should be used sparingly.

lgc.KX_ACTION_MODE_PLAY

The link you sent me was useful, but I’ve used it before. It lead to this…https://docs.blender.org/api/blender_python_api_current/bge.logic.html#gameobject-playaction-mode

I just used the variable number instead of the KX_ACTION_MODE_LOOP…

Okay, like usual. Experiencing instability(shakiness) with the animations. I’m guessing it must be one the animation properties?