tarray not adding a struct in C++

So, I’m trying to add a new struct to an tarray of said struct thru a function. But, when trying to get values from that tarray it returns nothing

The declaration of the tarray
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Status") TArray<FStatus> Status;

The Struct

USTRUCT(BlueprintType)
struct FStatus
{
GENERATED_USTRUCT_BODY()

FStatus() {};

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
FString Name;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
FString Description;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
USkillEffect* Effect;//TSubclassOf<class USkillEffect> Effect;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
FSkillContext Ctx;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
int32 Timer = -1;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
int32 MaxTimer = 5;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
int32 Stacks = 1;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
int32 MaxStacks = 5;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
bool bLinkTimerAndStacks=false;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Status")
EEffectsMode Mode = EEffectsMode::Disabled;


bool operator==(const FStatus& Other) const
{
	return (Name == Other.Name);
}

};

we’d have to see the function, things to check

  1. using the correct source
  2. using a Ref not a Copy

The problem is most probably in the adding, not in the struct itself.

Can you provide a snippet of adding and reading an element? (Can you try reading right after adding?)

This is the implementation of the function

void UStatusManager::AddStatus(FStatus NewStatus)
{

if(Status.Contains(NewStatus))
{ 
	int32 ID;
	Status.Find(NewStatus, ID);
	AddStack(Status[ID].Name, Status[ID].Stacks);

}
else
{
	UErrorManager::Get()->Log(TEXT("Add"), TEXT("Status: " + NewStatus.Name),
		EErrorType::Et_Display, true);
	int32 Index = Status.Add(NewStatus);

	UErrorManager::Get()->Log(TEXT("Find"), TEXT("Status: " + Index),
		EErrorType::Et_Display, true);
}
if (NewStatus.Mode == EEffectsMode::Beginning || NewStatus.Mode == EEffectsMode::BeginningEnd)
{
	ApplyStatusEffect(NewStatus.Name);
}

}

And this is where I’m using it

void UEntangleEffect::Apply(AActor* Target, const FSkillContext& Ctx, FSkillResult& OutResult)
{
IEntityAffectable* AffCaster = Cast(Ctx.Caster);
ATileEntity* Affectable = Cast(Target);

//FStatus EntangleS;// = CreateDefaultSubobject<FStatus>(TEXT("EntangleS"));

EntangleS.Ctx = Ctx;
EntangleS.Name = "Entangle";
EntangleS.Effect = Effect;
EntangleS.Timer = 1;
//EntangleS->Mode = EEffectsMode::EveryTick;

Affectable->GetStatusManager()->AddStatus(EntangleS);

OutResult.bSuccess = true;
OutResult.Affected.Add(Target);

}


A few issues:

It’s a struct. Just declare it on the stack: FStatus EntangleS;. Default subobjects are for constructors and UObjects.

  1. AddStatus should take a const & input. Just best practice, should actually cause you problems.

  2. AddStatus - So you check if the status is already present, if it is you update it. If it’s not, you log errors? You then call another function with only the name of the status effect. We can’t see what ApplyStatusEffect is doing, but it can’t be adding the FStatus to your array. How would any effect be added the first time? Why is adding an effect that doesn’t already exist an error? That seems like the thing that will happen most of the time. At no point (in the code you’ve shared at least) is it possible for an FStatus to be added to that array because the first instance will always fail the contains check.

  3. Don’t do contains + find. Just do Find and check the return value to know if the index is within the array. Imagine you have 1,000,000 status effects. Contains could check every million elements and then find is going to repeat the exact same checks.

There is a Status.Add(NewStatus) is in the middle of the of the logging

What’s the type of Status in Status.Find(NewStatus, ID) and Status.Add(NewStatus)?

Is it a pure TArray<FSatus> or is it custom class? Can you log the size of it after adding?

Make sure Status.Add(NewStatus)actually copies the properFSatus data in your container. I think @MagForceSeven is right to suspect that the effect is not actually added for some reason.

Try to debug the raw content of Status container after an addition. If it looks alright (like expanding), that means that the reading/application is failing.

(Also do all the things @MagForceSeven pointed out. I think EntangleS declaration is commented out.)

The Status is a pure TArray of FStatus
And the declaration for EntangleS end up on the header file after I tried to fix this on my on

Oops, my bad.

Are those logs showing up?

Have you stepped through this code with your IDE? Maybe you’re adding to one manager but the ‘get’ that’s empty is reading from a different one. Maybe something is removing it between the add and get.

Unfortunately I don’t think there’s anything anyone without access to your full project would be able to do to help you out (no I’m not asking for that sort of access). You’re going to have to debug it with breakpoints and perhaps more logs. What you’ve shared should work just fine (though my other pieces of feedback are still relevant for making it better).

Totally unrelated, but having something called “ErrorManager” handling things that aren’t errors is very unintuitive.

Ok, I made some more testing and I managed to get the variables of the struct in another part of the code and the system that does the auto removal of expired status seem to be working. So now the question is, why I’m not being able to read this variables in some parts of the code

Also, the ErrorManager is something that I inherited from the previous programmer, so it was not my choice

Ok, I think I found the problem.
The ErrorManager seemingly can’t read integers and one of the functions that I was trying to use the StatusManager in, wasn’t being used

(Ah, the joys of inherited code)