Changing Values of C++ Struct Variables in BP Child Having No Effect on C++ Logic

For my project, I’ve created a C++ class that has a struct in it. Let’s say that struct MyStruct has variables Value1, Value2, and Value3 in it. Then I create some BP children derived from this class, that are meant to have different values in their versions of MyStruct.

I haven’t yet figured out how to go about this the right way…the main thing I’ve tried is creating a struct variable of MyStruct type in BP, and giving it some default values. But when I play the game, those values are not used for the logic I wrote in the parent class. I also tried playing around with some of the Make, Break, and Set Members nodes in the Event Graph, but only the struct values in C++ are ever considered.

Is there something else I can do to make this work?

You can’t just create a struct variable in the BP, because this variable isn’t referenced anywhere in your code. You need to declare the struct variable in C++ and make it editable (EditAnywhere, EditDefaultsOnly, etc.). And then you can change values of this variable in different blueprints.

3 Likes

If you set the value of the struct property in Blueprint, the BP value should be accessible in c++ but with a few exceptions. This is because some logic (for example the c++ constructor) runs before anything Blueprint, including setting the properties from the editor panel values. Does it work if you read the properties in c++ on method “void BeginPlay” or “void NativeOnInitialized” ?

2 Likes

@Tuerer that is what op seems to be doing already.

2 Likes

You can’t just create a struct variable in the BP, because this variable isn’t referenced anywhere in your code. You need to declare the struct variable in C++ and make it editable (EditAnywhere, EditDefaultsOnly, etc.). And then you can change values of this variable in different blueprints.

I’d declared the struct variable in C++, but I’d neglected to include those specifiers along with it. Thank you both for helping me through this! I’m still very new, so not all of the basics are committed to memory yet.

After exposing the variable to Blueprint properly, changing class defaults and using Set nodes in the Event Graphs works. No need to create additional struct variables in BP, at least for this project.

If you set the value of the struct property in Blueprint, the BP value should be accessible in c++ but with a few exceptions. This is because some logic (for example the c++ constructor) runs before anything Blueprint, including setting the properties from the editor panel values. Does it work if you read the properties in c++ on method “void BeginPlay” or “void NativeOnInitialized” ?

Out of curiosity, what are the exceptions?

Well for example imagine a class "MyCProperty " with an int property “MyCProperty”. MyCProperty is exposed to Blueprints and can be edited on the editor panel. If you run the c++ constructor and modify the property like this:

UMyClass::UMyClass() {
  MyCProperty = 50;
}

MyCProperty will be 50 by default for both c++ and BP. At a later point (I don’t know exactly where but very very early) the value set on the BP editor will be read and the c++ property will be set to the BP value.

Long story short if you execute logic from the c++ constructor you will not yet access the values as overridden from the blueprint editor. When you run c++ logic on one of the gameplay events like “OnInitialized” or “BeginPlay” you are sure to use the overriden (bp set) values

1 Like

That makes sense. Thanks for the explanation!