How to access blueprint vars in c++?

How can I acces vars I’ve created in my Character’s blueprint in the c++ character class code?

There are 2 options:

  1. The simplest is to create a C++ base class, add the variables to that, and then create the BP derived from that class. That makes them very easy to access from C++. You will need to mark the variables in the header like this:

    UPROPERTY(BlueprintReadWrite, Category=MyCharacter)
    float MyFloat;

  2. It is possible to find a property by name and then access its value like this:

    UFloatProperty* FloatProp = FindField(Object->GetClass(), PropertyName);
    if(FloatProp != NULL)
    {
    float* FloatPtr = FloatProp->GetPropertyValue_InContainer(Object);
    if(FloatPtr != NULL)
    {
    float MyFloat = *FloatPtr;
    }
    }

1 Like

Thanks so much for the answer James!

You’re awesome!

:slight_smile:

Rama

UFloatProperty* FloatProp = FindField(Object->GetClass(), PropertyName);
if(FloatProp != NULL)
{
float* FloatPtr = FloatProp->GetPropertyValue_InContainer(Object);
if(FloatPtr != NULL)
{
float MyFloat = *FloatPtr;
}
}

I got this working!

I am using

SetPropertyValue_InContainer

to update my foot placement system

Is there an equivalent for UFloatProperty for vectors?

I tried UVectorProperty to no avail :slight_smile:

and where is the .h file for UFloatProperty?

I bet I can find lots of answers there if I know where to look in the header files

:slight_smile:

Thanks!

Ah, Vector is a Struct, so you need a UStructProperty, like this:

UScriptStruct* VectorStruct = FindObjectChecked(UObject::StaticClass(), TEXT("Vector"));
UStructProperty* VectorProp = FindField(Object->GetClass(), PropertyName);
if(VectorProp != NULL && VectorProp->Struct == VectorStruct)
{
	*VectorProp->ContainerPtrToValuePtr(Object) = Value;
}

Thanks so much James for taking the time!

I will be doing tutorial for rocket forum utilizing this info you are sharing with me,

as soon as I have good examples all set up!

-Rama

Like I said, in my opinion the easiest (and most efficient) way to do this is to add the variables in a C++ base class instead of finding them by name, but both work.

I tried doing that, but I could not figure out how to add variables to my AnimBlueprint in C++, or how to get custom variables from my extended Character c++ class into the Animblueprint:

https://rocket.unrealengine.com/questions/4932/how-do-i-extend-animblueprint-to-set-variables-via.html

:slight_smile:

Nvm, I figured out now, thanks James Golding!

tutorial on rocket forum:
http://forums.epicgames.com/threads/972861-Tutorial-Compile-C-for-UE4-Code-Samples-For-You-(New-Create-Material-Instances)?p=31653957&viewfull=1#post31653957

Hey James and Rama, this is exactly what I need but I don’t have a lot of experience with C++. Unfortunately I can’t acces to «http://forums.epicgames.com/threads/972861-Tutorial-Compile-C-for-UE4-Code-Samples-For-You-(New-Create-Material-Instances)?p=31653957&viewfull=1#post31653957». They say I don’t have permission to acces to this page. Just wanted to know if there a way for me to get the tutorial.

Thanks and have a good day.

Check this:

Hello, I’m a little bit late but these topic doesn’t exist anymore and i cannot find anything talking about that except here :confused:
If sometone get it, can you explain it to me please ?

explain what? How to access blueprint vars in c++? - #2 by ue4-archive

is probably the best answer on the topic.

Hi @eblade :smiley: FindField is deprecated (FindField | Unreal Engine Documentation)
it say "FindField will no longer return properties. Use FindFProperty instead or FindUField if you want to find UFunctions or UEnums. "

I don’t understand because i used FindFProperty and it returned nothing…

Without an example, it’s pretty difficult to advise. In general, though, attempting to do that is most likely the wrong way to achieve what you want to do.

If the C++ needs to access a variable that isn’t declared until later, then … you should move that variable.

	FProperty* P0 = FindFProperty<FProperty>(GroomAsset->GetClass(), GET_MEMBER_NAME_CHECKED(UGroomAsset, HairGroupsInterpolation));

and

if that helps inform you how to use it

1 Like