We'll be playing animations, control the animation playback type and control the animations from logic bricks.
Create a simple cube animation by either moving, rotating or scaling it. I've made an animation that lasts for 40 frames.
After making our animation, we go to Dope Sheet and rename our animation CubeAction to just jump for our convenience. Now let's move to the coding part.
In our main function create a parameter named cont for the controller, there's no need to define it for now.
Your function which is used by the controller will need 0-1 argument. If you have no argument you'll need to create it like this, which cannot be a good way to do everytime.
import Range
def main():
cont = Range.logic.getCurrentController() # Not a good idea :(
own = cont.owner
cont.activate(cont.actuators["lala"])
If you create your function with a single arugument you'll need to create it like this, which is a better way. If your code doesn't have any functions or attributes from Range there's no need to import the module.
def main(cont):
own = cont.owner
cont.activate(cont.actuators["lala"])
The reason why do we need only 0 or 1 argument is it accepts only one or you'll end up getting an error.
We'll need to play our animation now so we need the function playAction() and our code looks like this.
def main(cont):
own = cont.owner
own.playAction("jump", 0, 40)
Now our cube must be jumping. Jumps once if you haven't turned on true level triggering.
The playAction has parameters like this and we'll look at the basics for now. Or you can look at the docs if you want.
playAction(name, start_frame, end_frame, layer=0, priority=0, blendin=0, play_mode=KX_ACTION_MODE_PLAY, layer_weight=0.0, ipo_flags=0, speed=1.0, blend_mode=KX_ACTION_BLEND_BLEND)
Now we'll control our logic bricks with python, btw you can clear the python code.
Add an Action actuator and rename it as anim_actu,
we will select our jump animation and set the end frame with
python.
Your action actuators are bascially BL_ActionActuator
with with you can do a lot. To get our actuator we do like so.
def main(cont):
own = cont.owner
if cont.sensors["Always"].positive:
anim_actu = cont.actuators["anim_actu"] # Got our actuator
anim_actu.action = "jump" # Animation name
anim_actu.frameStart = 0.0 # Start frame
anim_actu.frameEnd = 40.0 # End frame
cont.activate(anim_actu) # Activating our actuator
And our logic bricks looks like this.
Now if we run the engine, our animation must be working.