I have a USTRUCT() declared like so in Weapon.h:
USTRUCT()
struct FWeaponData
{
GENERATED_USTRUCT_BODY()
...
UPROPERTY(EditDefaultsOnly, Category = Config)
int32 Priority;
};
As well as a reference to it in the class declaration:
UCLASS()
class WEAPONESSENTIALS_API AWeapon : public AActor
{
GENERATED_BODY()
public:
...
UFUNCTION(BlueprintCallable, Category = Accessors)
FWeaponData GetWeaponConfig();
...
protected:
UPROPERTY(EditDefaultsOnly, Category = Config)
FWeaponData WeaponConfig;
...
};
GetWeaponData() is an accessor method with the following implementation:
FWeaponData AWeapon::GetWeaponConfig()
{
return WeaponConfig;
}
The data in WeaponConfig is supposed to be set in the defaults of any blueprint that inherits from AWeapon. In the code of AWeaponEssentialsCharacter (in WeaponEssentialsCharacter.cpp), there is some code that attempts to access a member of the USTRUCT() that results in an EX_BAD_ACCESS:
void AWeaponEssentialsCharacter::ProcessWeaponPickup(AWeapon *Weapon)
{
if (Weapon != NULL)
{
if (CurrentWeapon == NULL) // No weapon is currently equipped
{
AWeapon *Spawner = ()->SpawnActor<AWeapon>(Weapon->GetClass()); // EXC_BAD_ACCESS here
if (Spawner) // Valid weapon
{
Spawner = CurrentWeapon;
int32 SwitchValue = Spawner->GetWeaponConfig().Priority;
...
}
}
}
}
Unless I wasn’t paying proper attention, I did not see anything in the tutorial that initializes FWeaponData WeaponConfig in the C++ code. However, either the member or the USTRUCT() is null, or the USTRUCT() itself is null. What seems to be the issue in the handling of the USTRUCT() in question?
All of the above code is from a project adapted from the following YouTube tutorial series: