Download

getting variables from a cpp/h file to be read by another cpp file

hi I have created a new AnimInstance because I want to include some custom variables in my animation graph, but I want to be able to edit them in my character.cpp file
so in my NewAnimInstance.h file ive got

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
float AnimationNumber;

and in my Player1Character.cpp i’ve written #include “NewAnimInstance.h”, but when I write AnimationNumber in the Player1Character.cpp, I get an error saying its not recognized. Im not that experienced with C++ so I dont know how to go about this, can someone help?

Please don’t take this the wrong way, but I’d suggest that you brush up on some introductory programming/C++ tutorials to get the basics under the belt.
As for your specific question, you first need a reference to an instance of your AnimInstance class, e.g.



AnimInstance = Cast<UYourAnimInstanceClass>(SkleletalMeshCompRef->GetAnimInstance());
AnimInstanc->AnimationNumber = 27.0f;


I tried that a little later after some research, I found this site

I followed it to the letter but its not recognizing UMyAnimInstanceClass in the character class.

PlayerCharacter.cpp



void APlayerCharacter::TurnAtRate(float Rate)
{
	// calculate delta for this frame from the rate information
	AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
	UMyAnimInstance * Animation = Cast<UMyAnimInstance>(Mesh->GetAnimInstance());
	Animation->AimRight += Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds();
}

MyAnimInstance.h


#pragma once

#include "Animation/AnimInstance.h"
#include "MyAnimInstance.generated.h"

/**
*
*/
UCLASS(transient, Blueprintable, hideCategories = AnimInstance, BlueprintType)
class UMyAnimInstance : public UAnimInstance
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
		float AimUp;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
		float AimRight;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
		bool Running;
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
		bool Jumping;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
                float AnimationNumber 
};

is there something im missing?

Sorry for this kind of basic question but, did you include MyAnimInstance.h in the PlayerCharacter.cpp?

Else, check the typo ^^’

**** me,
no I didnt,

I did at one point and I got it working, but I had to start over cuz of a crash. It wasnt mentioned in the tutorial so I didnt notice.
thnx

^^

Always check the basic stuff before everything else. If something is unrecognized it basicaly mean it didn’t find the declaration of the variable. And as the declaration of variable in C++ are often (not always) in the header file, you have to tell the cpp file which header file he should use. This is why you have to #include the corresponding header file where the required variable are.