I have a struct which I want to contain the class type and the category of the pickup item, here is the struct declaration
USTRUCT()
struct FEquipmentSlot
{
GENERATED_BODY()
UPROPERTY()
TSubclassOf<APickupItem> EquipmentClass;
UPROPERTY()
PickupItemCategory EquipmentCategory;
//default constructor
FEquipmentSlot()
{
EquipmentClass = APickupItem::StaticClass();
EquipmentCategory = PickupItemCategory::NONE;
}
//constructor
FEquipmentSlot(TSubclassOf<APickupItem> Class,
PickupItemCategory Category)
{
EquipmentClass = Class;
EquipmentCategory = Category;
}
};
Inside the UEquipmentComponent class, I declare a TArray to store the struct and a function to accept two parameters and use these parameters to construct the struct and add it to TArray
.h
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class PIXELGAME_API UEquipmentComponent : public UActorComponent
{
protected:
UPROPERTY()
TArray<FEquipmentSlot> EquipmentSlots;
public:
UFUNCTION()
void AddEquipmentSlot(TSubclassOf<APickupItem> PickupItemClass,
PickupItemCategory Category);
};
.cpp
void UEquipmentComponent::AddEquipmentSlot(TSubclassOf<APickupItem> PickupItemClass,
PickupItemCategory Category)
{
EquipmentSlots.Add(FEquipmentSlot(Class, Category));
}
So here is the problem. When I call the function, I just want to add a struct to TArray but the editor will crash. and here is the crash report
LoginId:03f47e504a2eb5f8da7ac29cdd75db63
EpicAccountId:76d5fa6cc5884994b61338ec5bfe3a49
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x000000b8
UE4Editor_PixelGame_0104!UEquipmentComponent::AddEquipmentSlot() [E:\UnrealProjects\PixelGame\Source\PixelGame\Components\EquipmentComponent.cpp:80]
UE4Editor_PixelGame_0104!ASword::Interact() [E:\UnrealProjects\PixelGame\Source\PixelGame\Items\Sword.cpp:26]
UE4Editor_PixelGame_0104!APixelGameCharacter::Interact() [E:\UnrealProjects\PixelGame\Source\PixelGame\Character\PixelGameCharacter.cpp:268]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll
I’m new to UE4 c++, and I search many answers but seems there is no right answer for this problem. I was stuck for
two days and I couldn’t find an answer. Can anybody save me, please? Any suggestion will be very very very helpful.