Setting Animation To Play With Python In Game Engine

Hello, I wanted to know if someone could help me write a script(I am very bad at scripting:().

The idea is that this script would change which animation a armature would play in game,

Not just walk\stand kind of change, But rather the actual logic brick settings depending on a property,
So for example the logic brick is set to play [A actions] if property [prop] is equal to 1,
And if property [prop] is equal to 2 it plays [B actions],
so that the player can be changed from [Player A] to [Player B] and each plays different actions

(I hope i explained this pretty good,if not here is a pic)


I have an “Actions” script (module actually) that triggers every time the armature[“Action”] property is changed. Basically, when it triggers is just does this


if own["Action"] == "Idle":
    own.playAction("Idle", 1, 61, blendin=5, speed=1, play_mode=1)

own is the armature.
play_mode=1 is looping.
play_mode=0 is play once.

All you need to do is change the “Action” property when you want the action to change and it will play the correct one based on your if statements. I can upload a more complete example if you would like.

Check the API for more information on “playAction.”

http://www.blender.org/documentation/blender_python_api_2_69_1/bge.types.KX_GameObject.html?highlight=playaction#bge.types.KX_GameObject.playAction

1 Like

First off, Thank you, This is very helpful,
Second, Yes I would like to see a more complete example,
Could also include a Blend file?
This would be helpful, Thanks in advance

  • BobSmith77

yep, the method using from redbaron seem good.

you can use property string rather than integer, so the property can have the same(or almost) name of the action.

Thank you, This is very helpful,
But could you please upload a more complete example?
As i said, I am not very good at scripting,

well , use a box that is really the “manager” (he manage physic and logic)
then parent the armature to this box (i guess you know already this)

then , just because you re not expert with python , manage what you can with bricks.
select the box, add a property “action” , and make it as string , call this first action “anything”, (is a action fake)
action [anything]

logic:
keyboard(W) -> and -> property = “walk”
keyboard(I) -> and -> property = “idle”

then select the armature,
add a property “action” , exactly as for the box.

add a always sensor with true pulse.
add a controller and a text:


#######
import bge
cont = bge.logic.getCurrentController()
own = cont.owner


boxAction = own.parent["action"]


if boxAction != own["action"]:
  own["action"] = boxAction
  if   boxAction == "idle" : own.playAction("idle", 1, 500, play_mode=1)
  elif boxAction == "walk" : own.playAction("walk", 1, 20,  play_mode=1)




#######

i hope to not have write things wrong, since is a method pretty new also for me :smiley:

this was helpful to me, as well. thanks. a very simple task, but will ruin your day if you don’t know what you’re doing.