How to add StaticMesh to struct?

Hi!

I have a class for player that has an array of my custom struct. I want to add StaticMesh to that struct but I also want to be able to set that struct member in editor (blueprint). Unfortunately, whenever I compile after adding EditAnywhere or BlueprintReadWrite, UE crashes (I’m using 5.0.3).

Here is my struct:

USTRUCT(BlueprintType)
struct FWeaponProperties
{
	GENERATED_BODY()
public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float TimeBetweenShots;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float CurrentShotCooldown;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		bool bIsHitScan;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float BaseDamagePerShot;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 ClipAmmo;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 LeftAmmo;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 ClipSize;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 MaxAmmo;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		class UStaticMesh* Mesh;
};

and here is how that array is declared:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Gameplay")
	TArray<FWeaponProperties> Weapons;

My general idea here was to be able to set different types of weapons and with that - each weapon can have different meshes.

What am I doing here wrong?

What am I doing here wrong?

You are using mesh as parameter :). Create Enum for gun types (Gun, Machinegun, Shotgun… etc).

UENUM(BlueprintType)
enum EWeaponType { Gun, Machinegun, RocketLaucher, Shotgun, BFG };

And…

USTRUCT(BlueprintType)
struct FWeaponProperties
{
	GENERATED_BODY()
public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float TimeBetweenShots;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float CurrentShotCooldown;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		bool bIsHitScan;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float BaseDamagePerShot;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 ClipAmmo;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 LeftAmmo;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 ClipSize;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 MaxAmmo;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		TEnumAsByte<EWeaponType> Mesh;
};

This should work fine. When struct is read, then just change mesh accordingly.

So there is no way to add StaticMesh to structure?

Your code works fine for me

It seems that the problem is with declaring the array of such structure? I commented that part in my player class and it worked, no crash. But after I removed the comment and the array was back again in that class, ue crashed while compiling.

I am not sure, to be honest. But I would still strongly suggest that you enumerate it. StaticMesh is way to complex and there is no need for it. Just identify the mesh using Enumerator.

Yeah, it sounds like a good idea to switch to enum. Thank you for the advice! :grin: