Having Trouble Creating a Struct for Weapons

Hi there, I’m trying to make a basic FPS where the player can switch weapons on-the-fly like most shooters. The research I’ve done seems to indicate that the best way to achieve this would be to create a struct that holds names of and references to each weapon, then create an array of that struct.

So I put in an FString variable and a pointer variable for the weapon class, but I get a series of errors pertaining to the weapon class when I try to compile and so far I haven’t been able to find out what the error means. The compiler has no issue with me declaring a UObject instead of a class pointer, but that doesn’t get me access to the class-specific functions, variables, etc. that I need.

Here is my struct and the errors I’m getting:

USTRUCT(BlueprintType)
struct FWeaponStruct
{
GENERATED_BODY()

UPROPERTY()
	FString WeaponName;

UPROPERTY()
	AHitscanWeapon* WeaponRef;

FWeaponStruct() : WeaponName(TEXT("")), WeaponRef(nullptr) {}

FWeaponStruct(const FString InWeaponName, AHitscanWeapon* InWeaponRef)
{
	
}

};

PlayerCharacter.h(18): error C2143: syntax error: missing ‘;’ before ‘
PlayerCharacter.h(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
PlayerCharacter.h(18): error C2238: unexpected token(s) preceding ‘;’
PlayerCharacter.h(22): error C2061: syntax error: identifier ‘AHitscanWeapon’
PlayerCharacter.h(20): error C2614: ‘FWeaponStruct’: illegal member initialization: ‘WeaponRef’ is not a base or member
PlayerCharacter.gen.cpp(73): error C2039: ‘WeaponRef’: is not a member of ‘FWeaponStruct’
PlayerCharacter.h(10): note: see declaration of ‘FWeaponStruct’
PlayerCharacter.gen.cpp(73): error C2618: illegal member designator in offsetof
PlayerCharacter.gen.cpp(73): note: offsetof has a builtin meaning; use /Zc:offsetof- to revert to old, non-conforming definition
[2/3] Compile PlayerCharacter.cpp
PlayerCharacter.h(18): error C2143: syntax error: missing ‘;’ before '

PlayerCharacter.h(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
PlayerCharacter.h(18): error C2238: unexpected token(s) preceding ‘;’
PlayerCharacter.h(22): error C2061: syntax error: identifier ‘AHitscanWeapon’
PlayerCharacter.h(20): error C2614: ‘FWeaponStruct’: illegal member initialization: ‘WeaponRef’ is not a base or member
PlayerCharacter.cpp(53): error C2039: ‘WeaponRef’: is not a member of ‘FWeaponStruct’
PlayerCharacter.h(10): note: see declaration of ‘FWeaponStruct’
PlayerCharacter.cpp(67): error C2039: ‘WeaponRef’: is not a member of ‘FWeaponStruct’
PlayerCharacter.h(10): note: see declaration of ‘FWeaponStruct’

I guess it’s because the compiler doesn’t know what AHitscanWeapon is.
Try

UPROPERTY()
class AHitscanWeapon* WeaponRef;

If it doesn’t work, you should probably #include "HitscanWeapon.h"

2 Likes

I guess it’s because the compiler doesn’t know what AHitscanWeapon is.
Try

UPROPERTY()
class AHitscanWeapon* WeaponRef;

Yeah, it turns out I just needed to forward-declare it! Thank you so much, this was driving me crazy!

2 Likes