Performing any action on a user-defined struct crashes the editor

Hi,
So I was meaning to create a Weapon slots system for a game I’m working on, and my idea was that I’d have a struct representing a Weapon Slot, which I could just add/attach to the character. This is the first time I have tried using USTRUCTS, and I can’t see to figure out the problem here.

  1. I created a C++ class with the parent “user defined struct”. This is the .h file for the same:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once
#include “CoreMinimal.h”
#include “PBaseWeapon.h”
#include “Engine/UserDefinedStruct.h”
#include “WeaponSlots.generated.h”

USTRUCT(BlueprintType)
struct FWeaponSlot {
GENERATED_USTRUCT_BODY()

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Details")
bool isEmpty;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Details")
FString WeaponName;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Details")
APBaseWeapon* WeaponBP;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Details")
int AmmoCount;

void SetIsEmpty(bool value) {
	isEmpty = value;
}

bool GetIsEmpty() {
	return isEmpty;
}

   // other getter and setter methods here

FWeaponSlot() {
	isEmpty = false;
	WeaponName = "";
	WeaponBP = NULL;
	AmmoCount = 0;
}

};

UCLASS()
class PAINTGAME_API UWeaponSlots : public UUserDefinedStruct
{
GENERATED_BODY()

};

This compiles just fine.
Now I added two FWeaponSlot variables on my character called Primary and Secondary Slots. In the character’s BeginPlay function, I do this :

PrimarySlot->SetIsEmpty(true);
SecondarySlot->SetIsEmpty(true);

Even this compiles and gives no errors, but as soon as I hit play, i.e. as soon as the character’s BeginPlay is executed, the editor crashes.

Any suggestion or a nudge in the right direction as to why this is happening or how to go about using Structs, is greatly appreciated.
Thanks!

Hi, can you post your crash log?

Hi, here it islink text

I don’t know how to properly read this, but I did try going through it. Where it mentions that PCharacter Line 175 had an error - that’s the line where I’m performing operations on the USTRUCT. If I comment those operations out, the game doesn’t crash.

Thanks again!

We might need to see a little more code to help with the issue, but my hunch is that you’re treating the variables as pointers to class objects rather than structs:

FWeaponSlot* PrimarySlot

where you should be using :

FWeaponSlot PrimarySlot

You then don’t need to dereference them with ->, but instead access directly with the dot notation. So your line:

PrimarySlot->SetIsEmpty(true); SecondarySlot->SetIsEmpty(true);

becomes

PrimarySlot.SetIsEmpty(true); SecondarySlot.SetIsEmpty(true);

Oh wow, that was it. I should’ve known I did something wrong in the .h file when every time I typed a period, VS automatically changed to ->.
Thanks a lot!