I originally wrote this for UT3, but it should be pretty much the same for UDK. It is intended for coders already familiar with UnrealScript.
As you may be aware, in UDK player animations are generally driven an AnimTree. This is created in the editor, and is composed of AnimNodes that select what animations are played based on the state of the player. It is possible to extend this system by creating custom AnimNodes using UnrealScript. In this example an AnimNode is created for switching animation paths based on the weapon the pawn is holding.
Firstly, the node itself:
So this node is a subclass of UTAnimBlendBase. This is a generic node which can have a number of child branches, and the active branch can be set via a function call. This subclass set the name for the child branches and prevents them being edited in the editor.
At this point you can already add the node to an AnimTree. But it will not do anything, as there is no code to alter the active branch.
Next you need to alter your pawn class to update the nodes.
The PostInitAnimTree function accesses the AnimTree to populate a list of all the MyAnimBlendByWeaponType nodes. This means you can have as many as you want in the tree, the code will be able to find and update all of them when the held weapon changes.
Specifically for this case, a WeaponAttachment subclass is needed to inform the pawn what type of weapon they are holding:
Each specific weapon attachment subclass then set the WeaponAnimType in its default properties. The WeaponAttachment class is replicated and attachment processed on client machines, so this ensures that the pawn's animations will always match their displayed weapon.
As you may be aware, in UDK player animations are generally driven an AnimTree. This is created in the editor, and is composed of AnimNodes that select what animations are played based on the state of the player. It is possible to extend this system by creating custom AnimNodes using UnrealScript. In this example an AnimNode is created for switching animation paths based on the weapon the pawn is holding.
Firstly, the node itself:
Code:
class MyAnimBlendByWeaponType extends UTAnimBlendBase; enum MyWeaponType { MWT_Sword, MWT_Gun, MWT_Bow }; simulated function SetWeaponAnimType(MyWeaponType weapAnimType) { SetActiveChild(weapAnimType, BlendTime); } DefaultProperties { Children(0)=(Name="Sword") Children(1)=(Name="Gun") Children(2)=(Name="Bow") bFixNumChildren=True }
At this point you can already add the node to an AnimTree. But it will not do anything, as there is no code to alter the active branch.
Next you need to alter your pawn class to update the nodes.
Code:
class MyAnimPawn extends UTPawn; var array<MyAnimBlendByWeaponType> WeaponTypeAnimNodes; simulated event PostInitAnimTree(SkeletalMeshComponent SkelComp) { local MyAnimBlendByWeaponType BlendByWeaponType; super.PostInitAnimTree(SkelComp); if (SkelComp == Mesh) { foreach mesh.AllAnimNodes(class'MyAnimBlendByWeaponType', BlendByWeaponType) { WeaponTypeAnimNodes[WeaponTypeAnimNodes.Length] = BlendByWeaponType; } } } simulated function SetWeaponAnimType(MyWeaponType weapAnimType) { local int i; for (i = 0; i < WeaponTypeAnimNodes.length; i++) { WeaponTypeAnimNodes[i].SetWeaponAnimType(weapAnimType); } }
Specifically for this case, a WeaponAttachment subclass is needed to inform the pawn what type of weapon they are holding:
Code:
class MyWeaponAttachment extends UTWeaponAttachment; var MyWeaponType WeaponAnimType; simulated function AttachTo(UTPawn OwnerPawn) { local MyAnimPawn AnimPawn; super.AttachTo(OwnerPawn); AnimPawn= MyAnimPawn(OwnerPawn); if (AnimPawn != none) { AnimPawn.SetWeaponAnimType(WeaponAnimType); } } DefaultProperties { WeaponAnimType=MWT_Sword }
Comment