TArray SetNum does not change number of elements in the property window

In my HUD base class I have a TArray of fonts.

The TArray element is a structure:

/**
 * @brief This structure contains information about the fonts used to display information.
 *
 */
USTRUCT()
struct FKFontStruct
{
	GENERATED_USTRUCT_BODY()

	/**
	<summary>The font</summary>
	*/
	UPROPERTY(EditDefaultsOnly, Category = Attributes)
	UFont* HUDFont;

	/**
	<summary>The font scale</summary>
	*/
	UPROPERTY(EditDefaultsOnly, Category = Attributes)
	float HUDFontScale;

	/**
	<summary>The font color</summary>
	*/
	UPROPERTY(EditDefaultsOnly, Category = Attributes)
	FColor HUDFontColor;

	/**
	<summary>The font type</summary>
	*/
	UPROPERTY(VisibleAnywhere, Category = Attributes)
	EKFontTypeEnum HUDFontType;

	/** defaults */
	FKFontStruct()
	{
		HUDFont = NULL;
		HUDFontScale = 1.0f;
		HUDFontColor = FColor::Black;
		HUDFontType = EKFontTypeEnum::KFT_Max;
	}
};

The number of elements is based on the EKFontTypeEnum::KFT_Max

UENUM(BlueprintType)		//"BlueprintType" is essential to include
enum class EKFontTypeEnum : uint8
{
	KFT_General 				UMETA(DisplayName = "General"),
	KFT_Debug		 			UMETA(DisplayName = "Debug"),
	KFT_MessageTitle		 	UMETA(DisplayName = "MessageTitle"),
	KFT_MessageText			 	UMETA(DisplayName = "MessageText"),
	KFT_InfoMessage			 	UMETA(DisplayName = "InfoMessage"),
	KFT_WarningMessage			UMETA(DisplayName = "WarningMessage"),
	KFT_CriticalMessage			UMETA(DisplayName = "CriticalMessage"),
	KFT_Normal				 	UMETA(DisplayName = "Normal"),
	KFT_Big					 	UMETA(DisplayName = "Big"),
	KFT_EnemyName			 	UMETA(DisplayName = "EnemyName"),
	KFT_Max					 	UMETA(Hidden)
};

The TArray initialization occurred in PostInitializeComponents().
I sets the number of array’s elements.

void AKHUD::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	// Sets the number of fonts
	HUDFonts.SetNum((int32)EKFontTypeEnum::KFT_Max);
	// and its type
	for (auto idx = 0; idx < (int32)EKFontTypeEnum::KFT_Max; ++idx)
	{
		HUDFonts[idx].HUDFontType = (EKFontTypeEnum)idx;
	}
}

If I had new fonts in EKFontTypeEnum, unfortunately the number of elements is not reflected in the property window.
If I had 4 font’s element and add 4 new, I’m still have 4 elements.
If I add 4 elements directly from the property window, when I relaunch the editor, all new 4 elements have KFT_Max as a font type.
It does not change its type even if the AKHUD::PostInitializeComponents() function is called and the loop executed correctly.

Is there another way to initialize a TArray and be reflected within the property window ?

Dominique

Hi Dominique,

The PostInitializeComponent function is something that is executed at runtime so any changes made to the UProperty in this function will not occur until you start the simulation, at which time you can check the object in the World Outline and see that the UProperty has been changed.

As far as being able to initialize it in a way that it is shown in the editor, the best bet would be to do so in the constructor as it is run at compile time. If you wish you can continue using a separate function for it but it’ll have to be a custom function that you call in the constructor.

If I have misunderstood something, or your simply require more assistance, please let me know and I’ll be happy to assist further.

Have a nice day

Hi ,

I had try already but it does not work. The number of elements and the type isn’t set.

As you can see in the picture captureue4-1 I have 11 fonts. The type is still not reflected.

I add more fonts

UENUM(BlueprintType)		//"BlueprintType" is essential to include
enum class EKFontTypeEnum : uint8
{
	KFT_General 				UMETA(DisplayName = "General"),				// 0
	KFT_Debug		 			UMETA(DisplayName = "Debug"),				// 1
	KFT_Normal				 	UMETA(DisplayName = "Normal"),				// 2
	KFT_Big					 	UMETA(DisplayName = "Big"),					// 3
	KFT_MessageTitle		 	UMETA(DisplayName = "MessageTitle"),		// 4
	KFT_MessageText			 	UMETA(DisplayName = "MessageText"),			// 5
	KFT_InfoMessage			 	UMETA(DisplayName = "Info Message"),		// 6
	KFT_WarningMessage			UMETA(DisplayName = "Warning Message"),		// 7
	KFT_CriticalMessage			UMETA(DisplayName = "Critical Message"),	// 8
	KFT_EnemyName			 	UMETA(DisplayName = "Enemy Name"),			// 9
	KFT_DamageMessage		 	UMETA(DisplayName = "Damage Message"),		// 10
	KFT_EnergyMessage		 	UMETA(DisplayName = "Energy Message"),		// 11
	KFT_PowerMessage		 	UMETA(DisplayName = "Power Message"),		// 12
	KFT_SpeedMessage		 	UMETA(DisplayName = "Speed Message"),		// 13
	KFT_Max					 	UMETA(Hidden)								// count = 14
};

, leave the editor, recompile, relaunch the editor, the number of fonts is still 11 as you can see in picture captureue4-2 and the type is still not reflected.

42470-captureue4-2.png

The number of element is 14 but in the editor properties’ window it is still 11.

Have a nice day,
Dominique

I think there may be parts of your code that I am missing and it would help to be able to see the entire class. Could you send me the code files for your project? It would help if you could attach the .cpp and .h files that you are using.

Hi ,
Ok please find the basic HUD class cpp and h files.

Dominique

link text

When you’re taking these screenshots, are you looking at these properties in the Details window in the main editor or are you looking at the Class Defaults in the blueprint editor? Also, what changes if you click the yellow arrows beside the elements in the property window?

I’m looking at these properties in the Class Defaults in blueprint editor, since in the main editor I don’t know how to see the properties of this HUD derived blueprint.

Something changes, I don’t know what, but now when I click the reset yellow arrow, it is working. It initialize correctly all properties. Before, when I was cliking the reset arrow, the array was reset to zero elements. Now, it resets to the correct number of elements and sets the correct type.

I don’t know what occurred since I don’t change the HUD code. I was working on something else.

Sorry for having taking your time. Thank you very much and have a nice day.
Dominique