PDA

View Full Version : Tutorial: Creating custom AnimNodes



immortius
01-04-2010, 07:59 AM
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:



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
}


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.


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);
}
}


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:



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
}


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.

Denny
01-04-2010, 08:05 AM
Awesome Immortius, I was always curious how you did that for Nb. :)

KyuIn
01-23-2010, 05:20 AM
Thanks immortius! it was very helpful!! and exactly I wanted!!
by the way I've got a error from Frontend with "MyAnimPawn class"

like so - > (MyAnimPawn.uc(18) : Error, Unrecognized type 'MyWeaponAnimType')

and I really want to make it work.. So please let me know how to fix it out :)

immortius
01-23-2010, 05:28 AM
Ah, sorry. There was a typo in the MyAnimPawn code, should have MyWeaponType instead of MyWeaponAnimType.

KyuIn
01-23-2010, 11:18 AM
Cheers Man!! thanks million!! : )

KyuIn
01-23-2010, 11:38 AM
sorry to bother you Immortius then I've got this one now..
I've tried something other operation, that I think similar, to fix it out though, I couldn't fix it at all kkk
so, if you don't mind help me once more please : )

---I've got this------------------------------------------------------------
MyWeaponAttachment.uc(11) : Error, Bad or missing expression in '='
---------------------------------------------------------------------------

Shelke
01-23-2010, 05:02 PM
Fantastic immortius! Thank you, this is really helpful!

immortius
01-23-2010, 06:06 PM
---I've got this------------------------------------------------------------
MyWeaponAttachment.uc(11) : Error, Bad or missing expression in '='
---------------------------------------------------------------------------

You're expecting a lot from me if I'm supposed to guess what you're doing wrong without you showing your code. :)

KyuIn
01-23-2010, 08:38 PM
Well.. I haven't had Programming discussion :o
I thought, it may make you to read much more kkk
And, the last post I made, It was my bad kk I worte your code with a mistake.

Anyway I tried to mix my code with your code up and when I changed class name
MyAnimPawn to MyPawn in MyWeaponAttachment.uc It makes errors


MyPawn

class MyPawn extends GamePawn;

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);
}
}


simulated event PostBeginPlay()
{
`log("My pawn is now alive, and well!");
}
defaultproperties
{
Begin Object class=SkeletalMeshComponent Name=SkeletalMeshComponent0

bHasPhysicsAssetInstance=True
bEnableSoftBodySimulation=True
bSoftBodyAwakeOnStartup=True


SkeletalMesh=SkeletalMesh'SimDate.Mesh.Imp02'
AnimSets[0]=AnimSet'SimDate.AnimSet.Imp01AnimSet'
AnimTreeTemplate=AnimTree'SimDate.AnimSet.Imp01Ani mTree'
PhysicsAsset=PhysicsAsset'SimDate.Physics.Imp01_Ph ysics'

End Object



Mesh=SkeletalMeshComponent0
Components.Add(SkeletalMeshComponent0)
}




MyWeaponAttachment

class MyWeaponAttachment extends UTWeaponAttachment;

var MyWeaponType WeaponAnimType;

simulated function AttachTo(UTPawn OwnerPawn)
{
local MyPawn AnimPawn;

super.AttachTo(OwnerPawn);

AnimPawn= MyPawn(OwnerPawn);
if (AnimPawn != none)
{
AnimPawn.SetWeaponAnimType(WeaponAnimType);
}
}

DefaultProperties
{
WeaponAnimType=MWT_Sword
}

god It's very confuse kkk thanks.

immortius
01-23-2010, 09:15 PM
Your WeaponAttachment class subclasses UTWeaponAttachment, which expects the pawn it is attached to to be a UTPawn. Your pawn class subclasses GamePawn, so is not a UTPawn. So the weapon attachments won't work with your pawn.

KyuIn
01-23-2010, 10:38 PM
Ah!!!!! So that why it was not working!! Thanks a lot!! now I can see it : )

polemos
01-29-2010, 05:50 PM
immortius can u help; I want to have multiple Animation sets how do i set weapons to use specific animsets?

Kelt'ar
09-16-2010, 12:25 AM
THis is beautiful! This $#!+ works!!!

Kelt'ar
09-16-2010, 09:31 PM
For some reason, My Idle animation for one of my weapons is not working...The character plays the rifle animation instead:

See pic:
http://img59.imageshack.us/img59/8070/weapprob.jpg (http://img59.imageshack.us/i/weapprob.jpg/)

Uploaded with ImageShack.us (http://imageshack.us)

THe one on the left is the Idle and the right is when the character is moving

Ideas anyone?

Kelt'ar
04-08-2011, 03:39 PM
bUMP! And the jump animation doesnt work...the char plays the run anim when falling.

DannyMeister
04-09-2011, 04:38 PM
bUMP! And the jump animation doesnt work...the char plays the run anim when falling.

Unless this has to do with immortius' sample code for a custom AnimNode you should make your own thread over on the Characters and Animation (http://forums.epicgames.com/forumdisplay.php?f=376) forum. When you do, make sure to include a screenshot of the anim tree.

/off topic. Thanks immortius:)

Kelt'ar
04-10-2011, 11:54 AM
I am using his custom animnode...It works perfectly until I try to rotate and then the animation goes to Rif...also I have the jump problem