Help with C++ structure pointers and blueprints

I have a structure as:

USTRUCT(BlueprintType)
struct Fattribute_data
{
      float value = 0.0f;
      float minimum_value = 1.0f;
      float maximum_value = 1.0f;
}

I wish to create many such struct variables in a blueprint class and set their values in Class Defaults, like health, mana, stamina, armour etc.

I wish to have a variable in C++ such as:

TMap<MyEnum, Fattribute_data*>

such that, when I create the struct variable(s) in Blueprints and pass them to a blueprint callable method, I wish to have those structs mapped inside that TMap.

But, what is happening is, when I create some Fattribute_data variables such as health, mana, stamina in blueprints and set their values, the function creates the TMap, but upon accessing any value of the TMap using any key, I get only the last struct’s values.

I do not get the values of other structs.

Please help me.

How to write a function that can create such a map where any blueprint struct can simply have it’s address mapped by a TMap variable such as that?

Thanks.

A couple of things here, if you want Blueprint to have access to the data you need to declare the members as UPROPERTIES. And, you’ll need a GENERATED_BODY() in the structure definition. So, like:

USTRUCT(BlueprintType)
struct Fattribute_data
{
     GENERATED_BODY()

      UPROPERTY(BlueprintReadWrite, EditAnywhere, Category=Data)
      float value = 0.0f;

      UPROPERTY(BlueprintReadWrite, EditAnywhere, Category=Data)
      float minimum_value = 1.0f;

      UPROPERTY(BlueprintReadWrite, EditAnywhere, Category=Data)
      float maximum_value = 1.0f;
};

And then… USTRUCTs, unlike UObjects, are not garbage collected. You probably want the map to contain their value. Otherwise, you’d have to manage their memory yourself with smart pointers etc or whatever. So, your TMap will just have the struct value and not a pointer.

UPROPERTY(BlueprintReadWrite, EditAnywhere, Category=Data)
TMap<MyEnum, Fattribute_data>
1 Like

Thanks for the help.

So, what if I do not wish to delete them from memory?

Like, those variables may be constructed in the Blueprint classes of an ActorComponent Class.

So, technically I want their lifetime to be throughout the scope of the ActorComponent.

That is, I may want to delete the structs from memory only when the ActorComponents are marked for deletion.

How do I handle purging the structs from memory in this case?
Is this a good design or practice?

You just make a TMap UPROPERTY, as above, without making it a pointer.

Then the TMap is allocated as part of the component, which is a garbage collected UObject.

And then, the TMap itself manages allocating and de-allocating your structures. It’ll allocate a new Fattribute_data when needed, and delete it when it’s not needed.

TMap, TArray, their jobs are to make developing easier sooo I always just let them do that.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.