Download

Blueprint usable List / Array referred to by string rather than index?

Hi,

I need to have a UPROPERTY(EditAnywhere, BlueprintReadWrite) with a data type similar to an array, but referred to by a name/string rather than an index. Not sure what the best way to go about this is.

In the blueprint editor I should be able to make a bunch of FMeshItem 's and give them a name and USkeletalMesh*, for example “Sword” and then chuck the sword skeletal mesh in there. This becomes a lot more important when I change the character’s sword item and it’s why I don’t want to refer to it as an array. This way when I break the array/list/whatever it’ll give me “Sword” or other.

I have this:


USTRUCT()
struct FMeshItem
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Merger")
		FString Name;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Merger")
		USkeletalMesh *Mesh;
};

And I have this:


UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Merger")
		*..an array/list..*<FMeshItem> MeshPieces;

Thanks for any help. I’m just trying to find the best way to go about this.

:confused:

That would work, just make sure to put USTRUCT(BlueprintType) so blueprint can recognize the struct you made.

TMap will do what you want, as well as a TArray of FMeshItems like you’ve come up with. However, TMap cannot be a property and is therefore not blueprint compatible, and using an array as-is will require you to manually iterate through it to find the mesh item you want.

I would go with the TArray, but use FName instead of FString for this kind of thing (faster compares). Then create yourself some helper methods that handle finding and returning the mesh:



USTRUCT(BlueprintType)
struct FMeshItem
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Merger")
		FName Name;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Merger")
		USkeletalMesh *Mesh;
};

UPROPERTY()
TArray<FMeshItem> MeshPieces;

UFUNCTION( Category="Merger", BlueprintPure )
USkeletalMesh* GetMeshPiece( const FName& MeshPieceName ) const
{
	for( const FMeshItem& MeshItem : MeshPieces )
	{
		if( MeshItem.Name == MeshPieceName )
		{
			return MeshItem.Mesh;
		}
	}
	
	return nullptr;
}