https://wiki.unrealengine.com/Animation_Blueprint,_Set_Custom_Variables_Via_C%2B%2B

Animation Blueprint, Set Custom Variables Via C++

Overview

Dear Community,

Here's the basic code you need to control the variables of your AnimBluePrint via c++ code. This is very useful if you just want to use the animblueprint for the actual skeletal
controllers or other nodes of interest to you, but you want to do all the calculations of what their values should be each tick via code. My example is a foot placement system!

It's much easier for me to do traces and get normals and account for various foot size offsets and max limb stretching etc via C++, so I wanted to set the Anim BP vars from
code
.

Extending AnimInstance

During Game Time an AnimInstance is created based on your AnimBlueprint, and it is this class that you want to extend to include your variables so you can easily edit them in C++
and get their values in the AnimBluePrint in the Editor.

Here's my code that I am using for my footplacement system:

YourAnimInstance .h

Here's an example of the kind of header you'd use for your extended AnimInstance class. Make sure to change the #include to your exact name! Also make sure to include some extra
spaces at the end of the .h and .cpp file so Visual Studio compiler is happy.

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "YourAnimInstance.generated.h"

UCLASS(transient, Blueprintable, hideCategories=AnimInstance, BlueprintType)
class UYourAnimInstance : public UAnimInstance
{
GENERATED_UCLASS_BODY() /** Left Lower Leg Offset From Ground, Set in Character.cpp Tick */
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category=FootPlacement)
FVector SkelControl_LeftLowerLegPos; /** Left Foot Rotation, Set in Character.cpp Tick */
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category=FootPlacement)
FRotator SkelControl_LeftFootRotation; /** Left Upper Leg Offset, Set in Character.cpp Tick */
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category=FootPlacement)
FVector SkelControl_LeftUpperLegPos;
};

YourAnimInstance .cpp

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#include "YourGame.h"

//////////////////////////////////////////////////////////////////////////
// UYourAnimInstance UYourAnimInstance::UYourAnimInstance(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
//set any default values for your variables here
SkelControl_LeftUpperLegPos = FVector(0, 0, 0);
}

Reparent Your AnimBluePrint

Now that you've added new variables you need to compile your C++ code to create your extended AnimInstance, and then load the editor and reparent your current AnimBluePrint to your
subclass:

Once you do this, you can now access your variables from your .h file, and their tooltip in the context menu will be your comment that you set in code!

Connect your custom variables to your anim node chain

The variables can be accessed via the right click menu now!

Accessing Anim Instance in C++

 Animation Blueprints are still blueprints,
you must access the instance of the blueprint per-Character.
This is the Animation Instance!
if(!Mesh) return;
//~~~~~~~~~~~~~~~ UYourAnimInstance * Animation =
Cast<UYourAnimInstance>( Mesh->GetAnimInstance() );
if(!Animation) return; Animation->YourInt32Var = 1200;

In-Depth Code Sample

Here's an example of accessing the Anim Instance from the Character class, which is where I am doing it for my foot placement system to easily access socket locations and rotations
etc.

Example Uses In C++ Code Character.cpp

//Never assume the mesh or anim instance was acquired, always check,
//or you can crash your game to desktop void AYourGameCharacter::ResetFootPlacement()
{
//No Mesh?
if (!Mesh) return; UYourAnimInstance * Animation =
Cast<UYourAnimInstance>( Mesh->GetAnimInstance() ); //No Anim Instance Acquired?
if(!Animation) return; //~~ Animation->SkelControl_LeftLowerLegPos = FVector(0,0,0);
Animation->SkelControl_LeftUpperLegPos = FVector(0,0,0);
Animation->SkelControl_LeftFootRotation = FRotator(0,0,0);
} void AYourGameCharacter::DoLeftFootAngleAdjustment(FRotator& FootRot)
{
//No Mesh?
if (!Mesh) return; UYourAnimInstance * Animation =
Cast<UYourAnimInstance>( Mesh->GetAnimInstance() ); //No Anim Instance Acquired?
if (!Animation) return; // //Set Animblueprint node rot
Animation->SkelControl_LeftFootRotation = FootRot;
}

Animation Blueprint, Set Custom Variables Via C++的更多相关文章

  1. piwik custom variables

    piwik custom variables  是一个功能非常强大的自定义变量跟踪方案,多用于基于访客或是页面级别的变量跟踪.piwik默认最多可以添加5个自定义变量. 使用方式是在客户端脚本里添加如 ...

  2. UE4]不使用角色蓝图、动画蓝图、状态机,用“24K纯C++”实现动画播放

    http://aigo.iteye.com/blog/2283454 原文作者:@玄冬Wong 不好意思,我稍稍标题党了,目前还不清楚如何用C++代码来实现BlendSpace和Montage的逻辑, ...

  3. [UE4] Adding a custom shading model

    转自:https://blog.felixkate.net/2016/05/22/adding-a-custom-shading-model-1/ This was written in Februa ...

  4. 《Note --- Unreal 4 --- behavior tree》

    Web: https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/index.html Test project: D:\En ...

  5. 从Unity引擎过度到Unreal4引擎(最终版)

    原文地址:http://demo.netfoucs.com/u011707076/article/details/44036839 前言 寒假回家到现在已经有十多天了,这些天回家不是睡就是吃....哎 ...

  6. Creating and using a blendspace in c++

    转自:https://forums.unrealengine.com/development-discussion/c-gameplay-programming/104831-creating-and ...

  7. [Java in NetBeans] Lesson 04. Class / Objects

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Class: Blueprint for an object. (e.g. dog is a class) Object: cust ...

  8. Web开发框架DevExtreme发布v18.2.5|附下载

    DevExtreme Complete Subscription是性能最优的 HTML5,CSS 和 JavaScript 移动.Web开发框架,可以直接在Visual Studio集成开发环境,构建 ...

  9. [UE4]C++设置AnimInstance的相关问题

    注意:ue4 4.17调用LoadObject<UAnimBlueprintGeneratedClass>直接崩 http://aigo.iteye.com/blog/2285001 UA ...

随机推荐

  1. linux c编程:make编译一

    一个工程中的源文件不计其数,按照不同的功能分类在若干的目录里面,makefile定义了一系列的规则,来制定那些文件需要先编译,那些文件后编译,那些文件重新编译.makefile最大的好处就是自动化编译 ...

  2. centos 7 PostgreSQL一些简单问题以及解决办法

    问题:org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are corre ...

  3. JVM性能分析工具详解--MAT等

    获得堆转储文件 巧妇难为无米之炊,我们首先需要获得一个堆转储文件.为了方便,本文采用的是 Sun JDK 6.通常来说,只要你设置了如下所示的 JVM 参数: -XX:+HeapDumpOnOutOf ...

  4. javascript控制样式表(不常用)

    <html> <head> <title>Example XHTML page</title> <link href="css1.css ...

  5. Contiki源码结构

    Contiki源码结构 apps目录下,用于存放Application,也就是我们的应用程序放在这个目录下.如webserver,webrowser等,如下图所示. core目录是contiki操作系 ...

  6. 白话 P-value

    准备再尝试一下,用大白话叙述一遍统计推断中最基础的东西(假设检验.P值.……),算是把这段时间的阅读和思考做个梳理(东西不难,思考侧重在如何表述和展示).这次打算用一种“迂回的”表达方式,比如,本文从 ...

  7. CSS3悬停特效合集Hover.css

    CSS3悬停特效合集Hover.css是一款特效丰富,支持2D变化特效.边框特效.阴影特效等: 使用简单,可直接复制相关特效代码,快速应用到元素上. 源码地址:http://www.huiyi8.co ...

  8. 夏日户外风景PSD素材

    夏日户外风景PSD素材适用于向日葵素材背景图设计 地址:http://www.huiyi8.com/xiangrikui/​

  9. URL过滤

    URL过滤 就是网址过滤.把不安全的.少儿不宜的.政治的东西过滤掉,访问这些网址就会提示受限,不能访问. 一.url过滤简介 针对企业对员工上网行为的控制管理,可以采用URL过滤技术.如企业不允许研发 ...

  10. redis数据

    毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...