Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unreal Engine 5 can't find TStaticArray

My problem revolves around a data asset class describing a survivor for my game. The class contains an FText for both the name and description and it compiled fine. However, after I added a property for teachable perks, which every survivor has 3 of, it started causing issues. At first, I thought I just forgot to include the necessary header, that being "Containers/StaticArray.h", but even after that I still get an error telling me that "a class, delegate, enum, or struct, with the name TStaticArray can not be found". I know that I have the correct header because I checked the documentation for the TStaticArray type so I'm stuck. It might be worth noting that intellisense does show the correct type, I can even navigate to the definition by holding down control and left-clicking on it in Visual Studio, but the compiler doesn't seem to find it.

Any help is much appreciated.

Here's the full code:

#pragma once

#include "CoreMinimal.h"
#include "Engine/DataAsset.h"
#include "Containers/StaticArray.h"
#include "SurvivorData.generated.h"

UCLASS()
class KILLERGAME_API USurvivorData : public UPrimaryDataAsset
{
    GENERATED_BODY()
    
public:
    UPROPERTY(BlueprintReadOnly, EditDefaultsOnly)
    FText Name;

    UPROPERTY(BlueprintReadOnly, EditDefaultsOnly)
    FText Description;

    UPROPERTY(BlueprintReadOnly, EditDefaultsOnly)
    TStaticArray<TObjectPtr<class UPerkData>, 3> TeachablePerks;
};

UPerkData is just another data asset describing a perk.

Tools used:

  • Visual Studio 2022 17.8.3
  • Unreal Engine 5.3
like image 632
Albert Wesker Avatar asked Oct 15 '25 20:10

Albert Wesker


1 Answers

So I think I've figured it out.

I had the correct header file and the template type doesn't matter. TStaticArray simply doesn't work as a UPROPERTY. Using it as a normal field works fine.

So that answers that.

No to my workaround to achieve the limiting of the array length to 3. I had to fall back to old faithful and use a simple TArray. I only care about limiting its size in the editor since it is a data asset. So I added a constructor that initializes the array with 3 empty slots, added EditFixedSize to the metadata, and overwrote the const version of the IsDataValid method derived from UObject. In it, I simply define the object as invalid should the array size not equal 3.

Here's the code:

Header:

#pragma once

#include "CoreMinimal.h"
#include "Engine/DataAsset.h"
#include "PerkData.h"
#include "SurvivorData.generated.h"

UCLASS()
class KILLERGAME_API USurvivorData : public UPrimaryDataAsset
{
    GENERATED_BODY()
    
public:
    UPROPERTY(BlueprintReadOnly, EditDefaultsOnly)
    FText Name;

    UPROPERTY(BlueprintReadOnly, EditDefaultsOnly)
    FText Description;

    UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, EditFixedSize)
    TArray<UPerkData*> TeachablePerks;

    USurvivorData();

    virtual EDataValidationResult IsDataValid(FDataValidationContext& Context) const override;
};

CPP:

#include "SurvivorData.h"
#include "Misc/DataValidation.h"

USurvivorData::USurvivorData()
    : TeachablePerks({ nullptr, nullptr, nullptr })
{ }

EDataValidationResult USurvivorData::IsDataValid(FDataValidationContext& Context) const
{
    if (TeachablePerks.Num() != 3)
    {
        Context.AddError(FText::FromString(TEXT("Number of teachable perks can only be 3!")));
        return EDataValidationResult::Invalid;
    }

    return EDataValidationResult::Valid;
}

For anyone curious, I'd encourage them to read the official documentation about data validation here.

So that wraps it up. TStaticArray is great for use as a private field or as a variable in code. Should you need an array for use in the editor however, blueprint or not, you should use TArray.

like image 173
Albert Wesker Avatar answered Oct 18 '25 08:10

Albert Wesker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!