Animation Blueprint, Set Custom Variables Via C++
来源:互联网 发布:java 商场类 项目结构 编辑:程序博客网 时间:2024/06/09 22:18
https://wiki.unrealengine.com/Animation_Blueprint,_Set_Custom_Variables_Via_C%2B%2B
Animation Blueprint, Set Custom Variables Via C++
Contents
[hide]- Overview
- Extending AnimInstance
- YourAnimInstance .h
- YourAnimInstance .cpp
- Reparent Your AnimBluePrint
- Connect your custom variables to your anim node chain
- Accessing Anim Instance in C++
- In-Depth Code Sample
- Conclusion
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 hereSkelControl_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 rotAnimation->SkelControl_LeftFootRotation = FootRot;}
- Animation Blueprint, Set Custom Variables Via C++
- piwik custom variables
- Windows Via C/C++:用户模式下的线程同步——Condition Variables 条件变量
- Google Analytics新增自定义变量 Custom Variables
- android set custom theme
- blueprint
- Blueprint
- BLUEPRINT
- blueprint
- Storage about C variables
- JavaScript quirk 7: inadvertent sharing of variables via closures
- Animation Set属性
- magento 1.4 -- 自定义变量(Custom Variables)使用初探
- 如何在piwik中获得用户变量(Custom Variables)
- Custom Animation——自定义动画
- Perl - How do I set environment variables
- Set Ubuntu12.04 LTS Envionment Variables
- Linux: 3 methods to set environment variables
- Mockjs
- Vncservice多用户登陆
- 【MDCC技术大咖秀】Android内存优化之OOM
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 解决方法
- Ubuntu 16.04 配置NFS
- Animation Blueprint, Set Custom Variables Via C++
- 把小数转化为百分比
- mysql 5.7修改密码
- 201709301535->unity类自运行
- Java 注解
- 基于Tesseract-OCR实现的JAVA WEB版OCR(图片转文字)
- 工作日记2017.09.30 Hibernate相关的问题
- 使用PLSql连接Oracle时报错ORA-12541: TNS: 无监听程序
- spring整合activemq问题小结