Custom Character Actions Using Montages - Epic Wiki

# Custom Character Actions Using Montages

Rate this Tutorial:

0.00

Approved for Versions:4.11

# Contents

# Overview

This tutorial shows a method of using animation montages to run custom actions for your characters. Typical usages for this would be as follows:

  • Part of a combat system - making your character do a jump-attack where root motion is necessary
  • Character interacting with something - such as pulling a lever
  • Custom animation event trigger, for cinematic-like breaks in your game

This tutorial is aimed at intermediate users who have an understanding of animation montages and animation instances. Currently this tutorial is setup to be used from C++ code, however it is possible to do a similar setup in Blueprint, however that is not currently covered.

Root Motion and Animation Montages are not covered here, but you can find them in the UnrealEngine documentation:

Root motion is covered here

Animation Montages are covered here

# C++ Code Portion

# First Steps

Outside of your character class, but included or in the same file, you will want to make an enum that contains your custom actions:

UENUM(BlueprintType) enum class EActions : uint8 { AC_Climb UMETA(DisplayName = "Climb"), AC_WallJump UMETA(DisplayName = "Wall Jump") };

Then you will want a delegate that takes an EActions value:

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FActionAnim, EActions, ActionUsed);

# Custom ACharacter Subclass .h

You will want to setup your custom ACharacter to contain the BlueprintAssignable UPROPERTY and BlueprintCallable UFUNCTION.

UCLASS() class MYGAME_API APlayer_Character : public ACharacter { GENERATED_BODY()     public: UPROPERTY(BlueprintAssignable) FActionAnim UseAction;   UFUNCTION(BlueprintCallable, meta = (DisplayName = "Use Action"), Category = "Character|Animation") FName UseAction_Implementation(EActions ActionUsed); };

# Getting the FName of an Enum Value

Our AnimInstance will want to know which animation to play, so we need to give it the value as an FName.

This function is taken from Epic's Kismet Libraries. It can go anywhere as long as you #include it.

static FName GetEnumValueAsName(const UEnum* Enum, uint8 EnumValue) { if (Enum != NULL) { int32 EnumIndex = Enum->GetIndexByValue(EnumValue); return FName(*Enum->GetEnumText(EnumIndex).ToString()); }   return FName(); }

# Custom ACharacter .cpp

Place this in your custom character .cpp:

FName APlayer_Character::UseAction_Implementation(EActions ActionUsed) { const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EActions"), true); return GetEnumValueAsName(EnumPtr, (uint8)ActionUsed); }

# Calling the Event from C++

When you want to use an action, simply call:

UseAction.Broadcast(EActions::AC_Climb);

# Animation Montage Portion

Because we are using the FName of the Enum Value to call a Montage Section, we will want to setup our Animation Montage using Section names identical to the DisplayName set for the EActions Enum.

(Note: The method will return "Climb" rather than "AC_Climb")

AnimMontage.png

# Anim Instance Portion

# Event Graph

Setup your event graph as follows, however we'll cover the "Custom Action Event" in the next section.

EventGraph.png

# Binding the Event

This is where we bind the event and tell it what we want it to do when it's broadcast. You can get the correct context for "Bind Event to UseAction" and "Use Action" by dragging off the Character pin.

Action BindEvent.png

# That's it!

Remember earlier when we called UseAction.BroadCast(EActions::AC_Climb) ? That's all you should have to do for it to play your custom animation.

Retrieved from "https://wiki.unrealengine.com/index.php?title=Custom_Character_Actions_Using_Montages&oldid=21165"

Category:

Hidden category: