Access blueprint components from c++

Hi.

I am working on a small plugin for the unreal editor and i need to have access a blueprints components from c++.

What I want is basically access to the variables in the components of the blueprint currently open in the blueprint viewer ( all the components in the blueprints component tab ).

So if I have, for example, a character blueprint containing a character movement component and a camera component I want to be able to access for example the “field of view” float value in the camera settings or the “Jump Z Velocity” float value in the character movement component from the c++ code.

Is this possible? I might add that i only want to read the values, not change any of them if that makes a difference.

I have access to the current blueprint as “UBlueprint* bp” and from other questions here I read that if I have a Actor pointer it is possible to call GetComponents that will return a list of components, is this what I am looking for? and if it is, how do I use this with my UBlueprint pointer?

Many thanks in advance.

The correct way to do this would be to first create a C++ class, than create a blueprint from that class.

For example, you create a class names ClassA. You specify that class to be Blueprintable by using the Blueprintable specifier in the UCLASS macro when defining your class.

Then, in the editor create your Blueprint from ClassA. Let’s call it ClassA_BP for this example.

One way to do it would be to create your component (like your movement component) directly in the C++ class using the ObjectInitializer (Or the PCIP if you are on a version of Unreal earlier than 4.6). If you really want to create the components in your Blueprint and then “get” them in your C++, then yes, the way to go is to use GetComponents().

Something like this : (I’m writing from memory, so the syntax may be wrong)

TArray<UMoveComponents*> allMoveComp;
GetComponents<UMoveComponent>(allMoveComp);

for(int32 i = 0; i < allMoveComp.Num(); i++)
{
    //Do whaterver you want here with the components...
}

The plugin should work with any blueprint so i do need to create the components in the blueprint and then get them in C++. So then GetComponents() is what i want, great. But if I have a blueprint pointer how can i use this function? If I have a character blueprint, how do I get a AActor from that I can use to call GetComponents() ?

I’m not certain this is the correct way, but I’d try getting the CDO as follows:

Blueprint->GeneratedClass->GetDefaultObject< AActor >() ;

Hopefully, if it’s an actor blueprint, that should return a valid pointer to the default object for the blueprint’s class, from which you can access the values.

Yes, this does give me all the default components, so it is a big step in the right direction. But i need to access all of the components in the current blueprint not only the default components and i have not yet figured out how to do this. Then off course i need to figure out how to access the values inside of these components, but one step at a time for now.

I’m not sure what you mean by ‘default’ components. I haven’t tested it, but the CDO for a blueprint should have all components which you can see in the components tab instantiated for it. Then you can access their values by either casting, or, if you don’t want to make any assumptions about the types of components, you can call GetClass() and then iterate over the properties.

You might also need to be aware that the CDO will only reflect the state of the blueprint at the point it was last compiled.

With ‘default’ components I meant the components that exist by default when I create for example a character blueprint. Any components i added later to the blueprint did not show up. But i figured out how to find those components in another day. I also managed to access the values so now I think that I have everything I needed. Thanks for the answer.

How did you do that man? i’m stuck on this too. Any suggestions?

I am also looking for a solution to this. Could you share the workaround you found?

Thanks!

While the asset is being edited, you might have luck accessing the editor’s preview instance using Blueprint->SimpleConstructionScript->GetComponentEditorActorInstance()

I find a solution, hope it’s useful to you

UIntProperty* Prop;
Prop = FindFieldChecked(component->GetClass(), “PropName”);
int PropName = Prop->GetPropertyValue_InContainer(component);