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. NFT是什么,有什么前景?

    去年 11 月,Crypokitties 的发布给加密货币的世界带来了风暴,有些加密猫的价格甚至涨到了 30 万美元,以太坊网络拥堵不堪,平均贡献了当时以太坊网络30%的交易额.当 Cryptokit ...

  2. LeetCode——Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  3. 剑指Offer:二叉树中和为某一值的路径【34】

    剑指Offer:二叉树中和为某一值的路径[34] 题目描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. ...

  4. Ubuntu/CentOS下使用脚本自动安装 Docker

    Ubuntu.Debian 系列安装 Docker 系统要求 Docker 支持以下版本的 Ubuntu 和 Debian 操作系统: Ubuntu Xenial 16.04 (LTS) Ubuntu ...

  5. HTML5/CSS3简易版俄罗斯方块游戏

    在线演示 本地下载

  6. Linux内核同步【转】

    本文转载自:http://blog.csdn.net/a775992553/article/details/8797710 Linux设备驱动中必须解决的一个问题是多个进程对共享资源的并发访问,并发访 ...

  7. java Regex匹配及解析文本

    用一个main程序展示下 public static void main(String[] args){       String text = "SSM<br>LOC<b ...

  8. zabbix simple check

    摘自: http://www.ttlsa.com/zabbix/zabbix-simple-checks/ 1. 开始 Simple checks通常用来检查远程未安装代理或者客户端的服务 使用sim ...

  9. [干货]兼容HTML5的Placeholder属性-更新版v0.10102013

    HTML5对Web Form做了许多增强,比如input新增的type类型.Form Validation等.Placeholder是HTML5新增的另一个属性,当input或者textarea设置了 ...

  10. densenet tensorflow 中文汉字手写识别

    densenet 中文汉字手写识别,代码如下: import tensorflow as tf import os import random import math import tensorflo ...