Download

Error, use of undefined type 'AWeapon' ?

So, i keep getting this error whatever i try to Spawn my weapon class using the following




DWGameCharacter.cpp // ACharacter subclass
////////////////////////////////////
// Inventory / Default Inventory
void ADWGameCharacter::DefaultInventory()
{
				FActorSpawnParameters SpawnInfo;
				SpawnInfo.bNoCollisionFail = true;
				AWeapon* NewWeapon = GetWorld()->SpawnActor<AWeapon>(DefaultInvWeaponClasses[0], SpawnInfo);
				WeaponsInInventory.Add(NewWeapon);

}

In your CPP try including the header of the file you are trying to spawn.

#include “Weapon.h”

That did it thanks!

Actually scratch that, UE4 Crashes as soon it calls the function

I tried using the forward declarations tutorial from Rama https://wiki.unrealengine.com/Forward_Declarations

So my ADWGameCharacter.h looks like


// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#pragma once


#include "DWGameCharacter.generated.h"

class AWeapon;

UCLASS(config=Game)
class ADWGameCharacter : public ACharacter
{
	GENERATED_UCLASS_BODY()

        AWeapon* CurrentWeapon
        // other non relevant vars
};




and the .cpp stays pretty much the same, yet when i try to compile i still get the error about using a undefined type, if i include the Weapon.h in my ADWGameCharacter.cpp i can compile fine but
The game crashes as soon DefaultInventory() is called

Without the call stack it is difficult to be sure, but most likely the GetWorld() call is returning NULL because you are calling that function at a time when the world cannot yet be determined.

Hehe Yeah i changed it to occur on BeginPlay and it doesnt crash anymore, So if i ever need to spawn / add another class i just include its header?

Yes, you need to include it’s header if you want to use it, not just spawn.

There may be a way of grouping all the game headers together into one file and just using that, but I’m not sure if that’s recommended. I know with my setup in the Beta build I didn’t seem to need to include all the headers and they were recognized, but I’ve had to add them all with the public release.