I’ve created a USTRUCT which is in the same file with an UClass(Abstract) UUserWidget.
Which is something like this:
USTRUCT()
struct FCurrentTimeTextElement
{
GENERATED_BODY()
UPROPERTY()
int8 VariantID;
UPROPERTY()
uint8 TimeManagerIndex;
UPROPERTY()
uint8 StartIndex;
UPROPERTY()
uint8 CharacterCount;
FCurrentTimeTextElement()
{
UE_LOG(LogTemp, Warning, TEXT("FCurrentTimeTextElement::FCurrentTimeTextElement() DEFAULT CONSTRUCTOR"));
TimeManagerIndex = 0;
StartIndex = 0;
CharacterCount = 0;
VariantID = 0;
}
FCurrentTimeTextElement(const uint8 InTimeManagerIndex, const uint8 InStartIndex, const uint8 InCharacterCount, const int8 InVariantID)
{
UE_LOG(LogTemp, Warning,
TEXT("FCurrentTimeTextElement::FCurrentTimeTextElement(), InTimeManagerIndex: %d InStartIndex: %d InCharacterCount:%d"),
InTimeManagerIndex, InStartIndex, InCharacterCount);
TimeManagerIndex = InTimeManagerIndex;
StartIndex = InStartIndex;
CharacterCount = InCharacterCount;
VariantID = InVariantID;
}
};
And at the UUserWidget class I have an array from this struct like:
private:
UPROPERTY()
TArray<FCurrentTimeTextElement> CurrentTimeTextElementArray;
I also have this method to get a reference to it:
UFUNCTION()
TArray<FCurrentTimeTextElement>& GetCurrentTimeTextElementArray() { return CurrentTimeTextElementArray; }
So my issue is, SOMETIMES (Especially after a crash because of an assert) My code starts to produce more ustruct elements on my array. I don’t create them with any code. I’m probably somehow copying a default value by accident. Because my Default Constructor prints into log 4 default elements.
The fun part is, when I comment things one by one, and uncomment, it starts to act as it has to be (Not creating elements into array that with default constructor)
So what I thought is that I’m missing something related to Garbage Collection maybe? I’m a beginner with C++ and Unreal Engine so any help appreciated.
In case you need more data, here is where and how I access this array:
Here is something inside a method to find an element from this struct array:
const FCurrentTimeTextElement* CurrentTimeTextElement = CurrentTimeTextElementArray.FindByPredicate(
[Index](const FCurrentTimeTextElement& TimeTextElement)
{
return TimeTextElement.TimeManagerIndex == Index;
});
//TODO: Change this null check logic soon
if(CurrentTimeTextElement == nullptr)
{
return;
}
Here is how I add new elements to array at constructor of my UUserWidget:
CurrentTimeTextElementArray.Emplace(
FCurrentTimeTextElement(
FMathf::Abs(MatchedNumber), 0, CharacterCount, VariantID));
And this is the place I suspect I create copies without intention (But it starts to work appropriately again after commenting and uncommenting everything with compile each time:
const TArray<FCurrentTimeTextElement>& Elements= TimeControlsWidget->GetCurrentTimeTextElementArray();
Thanks in advance for help!