Editor crashes when adding a new instance of custom class in c++

Hi everyone,

I want to implement an inventory system in my project. To do so, I created a c++ class which derives from AActor and its only purpose is to control an array. Then I want to create an instance of that class inside my character’s class. First I declare the new variable in the .h file of my character class using:

class ARTzCharacterInventoryActor;

and

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "RTsGame")
ARTzCharacterInventoryActor* inventory;

Then I declare the new instance in the .cpp file of my character class:

APlayerCharacter::APlayerCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	inventory = NewNamedObject<ARTzCharacterInventoryActor>(this, FName("CharacterInventoryController"));
}

When I do this, the editor crashes when reaching about 92% loading with no crash report. I’ve tested this using UE 4.8 and it happens again. I know crashes occur when I add the declaration of the new instance since everything goes back to normal when I comment that line. What am I doing wrong? Is there another way to do this?

Thanks in advance!

Hello DRestoy

From a trouble shooting standpoint I would suggest first checking to ensure your crash is occurring at the line you set the inventory to “NewNamedObject”. If you comment out that line and the editor opens up without a problem then you know that is your problem area. My hunch is that that is a part of your problem.

If that is indeed the issue Change your Player Character to use the ObjectInitializer

APlayerCharacter::APlayerCharacter(const FObjectInitializer & ObjectInitializer)
 {
      // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
     inventory = ObjectInitializer.CreateDefaultSubobject<ARTzCharacterInventoryActor>(this, FName("CharacterInventoryController"));
 }

not entirely sure why but I recall having trouble using the standard ConstructObject functions in Constructors.

If you want to create an inventory that is editable via edior, you should derive it from USceneComponent instead of AActor. Thus you be able to attach one to your actor:

//declaring inventory class
UCLASS()
class UMyInventory : public USceneComponent {
     /* Do stuff here */
}

//in .h file of your Actor:
public:
UMyInventory* inventory;

//in .cpp of your Actor:
APlayerCharacter::APlayerCharacter()
 {
     inventory= CreateDefaultSubobject<UMyInventory>(TEXT("Inventory"));
 }

Besides the better code handling and using of flexible component system, this solution possibly should eleminate your crash problem because it is more transparent way than a creating AActor-deriving subclass with attaching to another actor (IMO).

Your log file probably has an indication of why the editor crashed. look under /Saved/Logs/…

Yes, as I said, I commented that line and then the editor loads with no problem. I tried using ObjectInitializer as you mentionet but the result is the same. The project can be compiled in Visual Studio but the Unreal Editor keeps crashing.

This worked like a charm. Thank you very much!!

I have a question though: my inventory class is not declared inside my character class files but as a separated .h and .cpp files so I can use that structure in other classes of my project. If I want to use “URTzCharacterInventoryActor* inventory;” declaration in the .h file of my character, I must do something like this:
class URTzCharacterInventoryActor;

UCLASS(Blueprintable)
class PRUEBAS2_API APlayerCharacter : public ACharacter
{
	GENERATED_BODY()

                     ...

The question is: is this always necessary to declare a variable of a custom class?
Thanks!!

Good to know, should be very helpful next time. Thanks!!

lol of course thats why it was not working the object was derived from Actor (naming conventions should have given it away). I did not catch that one. You need to Spawn Actors while UObject and Actor Components can only be created with ConstructObject functions or the ObjectInitializer. Also you you cannot spawn Actors from a constructor. SceneComponent (or ActorComponent) is the right choice.