Download

Referencing 'Variables' from the AnimBP in a C++ script?

Hi again folks. I am still struggling with my first animation setup. I am making a Point-N-Click game, and so I am not really using the Physics Simulation or MovementComponent. I need the user to click a part of the screen and the character walk towards it.

Normally when I write something like this, I make an enum called “MovementState {IDLE =0, WALKING = 1, GRAB = 2 etc etc}”.

This problem I have is that I have set up the State Machine in Blueprint (AnimBP), but I don’t really see any decent way to use Blueprint to determine the current state. I would much rather use my enum and simply set an “Int anim_state” that I created in the BP via the script where I am already handling the movement state.

I’ve been studying some working blueprints, but the ones I understood all used blueprint to get access to the current player pawn, and from there they figure out the movement from the movementComponent.

As I say, I’d rather just have a line of code somewhere in my existing switch statement which sets that one variable inside my State Machine. Picture below may help to explain what I mean:


In Material Editor , you have something like “SetScalarParameter” node that I could reference from my Cpp. But I do not see the AnimBP equivalent.

Sorry to keep asking about these things. But I’ve honestly been sat reading the docs and examples for many hours without really getting anywhere on this particular problem.

Thanks for all help as always.

Once you have pointer to AnimBP, that depends on how the variable was created.

If you defined it on a c++ AnimBP baseclass then you can Cast< T >() and access the enum directly;

If not, you have to use reflection system to modify the value from c++.

Search engine source code on examples how to use FindUField()

https://docs.unrealengine.com/4.27/en-US/API/Runtime/CoreUObject/UObject/FindField/2/

1 Like

Thanks for the fast reply! I am taking a look at the link but it says ‘deprecated’ just to let you know.

To what you said about where the variable is created. I could always do it another way, but so far I just created the int Anim_state from inside the AnimBP. . I already have a variable with the same name in my cpp code that is working . You see he can only be in one of 4 anim states at any time, never blending, never any other states. So the enum is already basically a state-machine in my code.
So therefore I just want to ‘copy’ that value from my c++ code into the Blueprint on that I shown in the red circle in my picture.

If I create a “C++ AnimBP baseclass” as you say, does that mean to make a class that extends AnimInstance? Would you mind giving me more detail of how I should create the “C++ Anim BP”.

(Thanks for all your help Bruno you have saved me lots of time already with your kind help)

I can’t give examples now because I am far away from my Pc, I’m on phone.

But yes, you can create an anim class in C++ and assign to your C++ character class by default. Then do all the code side of the AnimBP within C++, while building state graphs on the visual editor.

Ok that sounds exactly what I need. I will try to figure it out. All the docs tend to show is making it in blueprint, but perhaps I have to dig further. At least I now know it is possible to do that.

There’s some videos with code samples for creation of C++ anim base classes, like this one might help:

1 Like

Well the video seems good. But when I do exactly the same as him I still cannot find my reference anywhere to “animation_state”. The new AnimBP I created is parented to my extended AnimInstance class, and in there is blueprintable custom variable called animation_state. But it is nowhere to be seen in the blueprint UI.

#pragma once

#include "CoreMinimal.h"
#include "Animation/AnimInstance.h"
#include "AnimInstance_Player_PnC.generated.h"

/**
 * 
 */
UCLASS(transient, Blueprintable, hideCategories=AnimInstance, BlueprintType)
class UAnimInstance_Player_PnC : public UAnimInstance
//class VOLCANOTOWN_API UAnimInstance_Player_PnC : public UAnimInstance
{
	GENERATED_BODY()


public:

	UAnimInstance_Player_PnC();

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Daves")
	int animation_state;

	virtual void NativeUpdateAnimation(float DeltaSeconds) override;
	
};

Ah I apologise. It is there, I have to Right-Click to find it at the bottom (Sorry for the trouble :smiley: )

Full solution here just in case it helps someone in future. Basically I already have an Enum something like: MovementState {IDLE = 0, WALK = 1, RUN = 2, GRAB = 3}; inside my player class. Also an int variable int movement_state = 0; . Code already is partly in place making the player move etc (but until now without animation).

So I create a C++ class called “AnimInstance_player” and inside the AnimBP (shown in pics in posts above) I went in and ‘reparent’ it to my C++ class. After all that is done, here is the code. It was actually very easy to keep the AnimInstance C++ code “self-contained” because it can find my “movement_state” int on its own (no linking or no reference to it from the players side).

#pragma once

#include "CoreMinimal.h"
#include "Animation/AnimInstance.h"
#include "AnimInstance_Player_PnC.generated.h"

/**
 * 
 */
UCLASS(transient, Blueprintable, hideCategories=AnimInstance, BlueprintType)
class UAnimInstance_Player_PnC : public UAnimInstance
//class VOLCANOTOWN_API UAnimInstance_Player_PnC : public UAnimInstance
{
	GENERATED_BODY()


public:

	UAnimInstance_Player_PnC();

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Variables")
	int animation_state;

	virtual void NativeUpdateAnimation(float DeltaSeconds) override;
	
};

and the .cpp:

#include "AnimInstance_Player_PnC.h"

#include "Player_PnC.h"

UAnimInstance_Player_PnC::UAnimInstance_Player_PnC() 
{
	animation_state = 0;
}

void UAnimInstance_Player_PnC::NativeUpdateAnimation(float DeltaSeconds)
{
	Super::NativeUpdateAnimation(DeltaSeconds);

	APlayer_PnC* owningActor = Cast<APlayer_PnC>(GetOwningActor());


	if (owningActor != nullptr)
	{
		animation_state = owningActor->movement_state;
	}
}