Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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;


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

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

      // 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!

Product Version: UE 4.7
more ▼

asked Jun 24 '15 at 05:33 PM in C++ Programming

avatar image

3 1 2 4

avatar image HollingsworthDan Jun 24 '15 at 07:42 PM

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

avatar image DRestoy Jun 24 '15 at 09:06 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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
 class UMyInventory : public USceneComponent {
      /* Do stuff here */
 //in .h file of your Actor:
 UMyInventory* inventory;
 //in .cpp of your Actor:
      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).

more ▼

answered Jun 24 '15 at 05:47 PM

avatar image

61 1 1 5

avatar image DRestoy Jun 24 '15 at 09:04 PM

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;

 class PRUEBAS2_API APlayerCharacter : public ACharacter

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

avatar image ArcainOne Jun 24 '15 at 09:22 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Jun 24 '15 at 05:45 PM

avatar image

489 32 25 57

avatar image DRestoy Jun 24 '15 at 08:55 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question