UPBGE 0.2.5 : Add Animations To player using python

Hello,
I am learning UPBGE with python. I have created this demo project in UPBGE using Python. But I don’t know where the problem is in my code and why it’s not working

code:

import bge
from bge import logic, events
from mathutils import Vector

tc = logic.keyboard.inputs
mc = logic.mouse.inputs

def start(cont):
    own = cont.owner
    
    own["armature"] = [obj for obj in own.children if "armature" in obj] [0]
    own["armature"].playAction("Ani_1", 0,258,)# Player idle Animation
        
def update(cont):
    
    scene = bge.logic.getCurrentScene()
    cont = bge.logic.getCurrentController()
    own = cont.owner
    
    w = cont.sensors["W"] # keyboard sensor
    propfire = cont.sensors["Fire"] # property sensor. property < 40
    propreload = cont.sensors["Reload"] # property sensor. property > 40
    propreload0 = cont.actuators['Property'] # property actuator. property 'assign' 0
    
    if tc[events.WKEY].values[-1]: # keyboard, W_KEY
        own["armature"].playAction("Ani_1", 259,279,) # Player Movement Animation
        
    
    if propfire.positive: # property sensor. property < 40
        if mc[events.LEFTMOUSE].values[-1]: # LEFT_MOUSE_Button 
            for ob in scene.objects:
                if "ammo" in ob: # object Property
                    ammo = own["ammo"]
                    own["ammo"] += 1
                    print(ammo)
            own["armature"].playAction("Ani_1", 280,316,) # Player Fire Animation
            
        else:
            own["armature"].playAction("Ani_1", 0,258,)# Player idle Animation

    if tc[events.RKEY].values[-1]:# keyboard, R_KEY,
        own["armature"].playAction("Ani_1", 317,353,)# Player Reload Animation
        frame = own["armature"].getActionFrame() # Reload Animation Frame Count 
        print(int(frame))
        
        if frame >= 352:
            cont.activate(propreload0)# property actuator. property 'assign' 0
            
        else:
            cont.deactivate(propreload0)# property actuator. property 'assign' 0
                          
              
    elif propreload.positive: # property sensor. property > 40
        own["armature"].playAction("Ani_1", 317,353,) # Player Reload Animation
        frame = own["armature"].getActionFrame() # Reload Animation Frame Count
        print(int(frame))
        
        if frame >= 352:
            cont.activate(propreload0)# property actuator. property 'assign' 0
            
        else:
            cont.deactivate(propreload0)# property actuator. property 'assign' 0
  
    else:
        
        own["armature"].playAction("Ani_1", 0,258,) # Player idle Animation
            

demo file:

https://drive.google.com/file/d/1N_CEhfyZu9KF5LLUfWFQICzqXd3rCiBQ/view?usp=sharing

I apologize.
I am using my native language and translate to English.

try setting debug info using properties and enabling ‘display debug properties’

‘print’ is also useful - find out if you have a error using console and if not, try and figure where what is happening branches from what you expect,

sometimes it’s conflicts of animation layers that are hard to debug also*

1 Like

Hi. Your Google Drive link seems to be set to private. Please consider changing its file permissions to allow public accessibility.

Also, always check the Python Console for errors when programming in Blender.

image

1 Like

I apologize:

plz try this one

everything correctly working. but the problem is if i click the W_KEY and R_KEY movement and reloading animation is not working.

I apologize for the video resolution:

video:

Your reloading animation works fine for me, as does the idle animation.

The walking and firing animation does not for me, however. I think you need to setup some action priorities. Otherwise your animations are playing the same action simultaneously, at different frames.

1 Like

everything correctly working. but the problem is if i click the W_KEY and R_KEY movement and reloading animation is not working.


Can you fix this for me:
I have been working on this for 3 days. i am learning python and i dont know where i am having an issue? can you please fix this for me and tell me the exact issue? TIA

  • See if it has anything to do with the animation itself : difference of length, number of channels, same channels…

  • Since there’s a “stopAction” function maybe python doesn’t like that you stop an action by playing another one.

  • Check out that “priority” option with that “playAction” function. With that you can over-write with no problem. But I don’t know if the underdog will resume if it is longer . You have to investigate deeply and precisely the bhavior behind

  • Interesting fact i noticed : replaying the same animation but at different Start or End is ignored by the Action Actuator since there’s no animation replacement since there’s no change (coz same name) … maybe python has some zone of confusion there too? … I don’t know.

  • I guess you are spamming this code at each logic tic (didnt check) … that architecture is ok for computing realtime math , but dealing a complex action system with that is a receipt of a disaster . It’s a bad habit that we are seeing around for years … the game architecture will end up being a total garbage and impossible to debug once the game is becoming complex. Launch a piece of code at a precise event only .
    → So … if you see basic setup running at 20-30 fps … warning warning … this just cannot be!

  • Careful in general with property sensors( frame) that are the result of your direct actions(buttons)

  • maybe you don’t know this, but you can also use python to change the parameters of the logic bricks … and since the bricks run faster (coz C/C++ behind) than python code … If you master the comprehension of bricks + some clean python code , your get the best of 2 worlds. With hybrid solution, you will get everything done with 2 sensors, 1 action actuator and 1 python control of 10 lines of code…
    …whilst using either 100% bricks or 100% python and you end up either with a building or a bible.

Pure python is good for stuff that doesn’t exist with bricks like AI or some matrix computations, but for fast-running things like actions or any sensors, bricks are your friends .

http://bgepython.tutorialsforblender3d.com/ActionActuator

2 Likes

It works better when you change all:

  else:
            own["armature"].playAction("Ani_1", 0,258,)# Player idle Animation

to

  elif not own["armature"].isPlayingAction():        
            own["armature"].playAction("Ani_1", 0,258,) # Player idle Animation
1 Like

(reply to all - not to me - there’s 2 buttons)

1 Like

It works better when you change all:

  else:
            own["armature"].playAction("Ani_1", 0,258,)# Player idle Animation

to

  elif not own["armature"].isPlayingAction():        
            own["armature"].playAction("Ani_1", 0,258,) # Player idle Animation
1 Like

this happen because you dont blendin you animations in script - you animation always play how use only first layer - armature.playAction(“Reload_Gun”, 0, 100, blendin=5, layer=20), armature.playAction(“Player_Walk”, 0, 50, blendin=5, layer=1) - use more blendin, layers and setup for mixing animation in game or looking API - for understand how mixing you animation

1 Like