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"

C++ UPROPERTY actor component variable being set to null

I posted another question here https://answers.unrealengine.com/questions/737973/reference-being-added-to-defaults-container.html which is likely related and sharing the same root cause.

In short, I have a custom UActorComponent called UCreatureActionHandler (to handle actions as objects determining what is currently being done and what can and cannot be done by my character) being added to my custom ACharacter class, and using the UPROPERTY specifier in the variable declaration seems to cause sporadic, hard to track issues (mainly the component being null in BP, but also the above linked bug).

My code is as follows:

 //In ABaseCreature_Masterclass.h
 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Components") //This line is the problem
 UCreatureActionHandler* CreatureActionHandlerComponent;

     PrimaryActorTick.bCanEverTick = true;
     CreatureActionHandlerComponent = CreateDefaultSubobject<UCreatureActionHandler>(FName("CreatureActionHandlerComponent"));

The component UCreatureActionHandler is a simple C++ component inheriting from UActorComponent with default constructor, tick, and beginplay that comes with the class (there is more code but I commented everything out in the process of trying to find what was causing the bug, with no luck). As it stands now, the component does not have anything added to it that could cause an issue.

The problem presents itself in the form of the CreatureActionHandlerComponent variable being null. In the Blueprint defaults the window will be empty and blank when the component is selected and trying to get a reference to it via BP will return nullptr. However the component itself is working and present (e.g. printing from its tick function will show up), but the variable in the character is wiped clean.

A few quirks I have noticed:

-Changing the name of the variable will temporarily fix it (has lasted several weeks before) but it will break again whenever changes are made (exactly what changes I am unsure of)

-Changing any specifiers of the UPROPERTY in the variable declaration will temporarily fix it (usually until the next engine restart)

-Seems more prone to breaking if delegates are added, removed, or altered (altering delegates may also fix it until next compile/restart)

If I remove UPROPERTY from the variable declaration in ABaseCreature_Masterclass.h the component will work fine (but not be blueprint accessible in the same way, of course). This is a temporary fix that works but is not ideal since it makes it more annoying to work with the component. I have also noticed that I am not alone having this issue: https://issues.unrealengine.com/issue/UE-18284 is similar (though mine is not caused by a class change) and https://answers.unrealengine.com/questions/734317/custom-uactorcomponent-is-null.html who also found that removing UPROPERTY fixed the issue.

I have tried replicating this in a fresh project but I have had no luck with it (which is why I didn't post this as a bug report). However I have quadruple checked everything and there is no code affecting this component and its variable in the character that is not present in my replication attempts. While I could migrate the entire project to a new blank slate, it would be quite cumbersome not to mention there is no telling if the issue may return in the near future or not regardless.

I have tried basically everything by now, and while I could circumvent this issue by removing the UPROPERTY specifier it also makes it considerably more annoying to work with delegates in the component.

Product Version: UE 4.18
more ▼

asked Dec 26 '17 at 08:45 PM in C++ Programming

avatar image

30 2 2 6

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

1 answer: sort voted first

Encountered the same issue on UE4.21.1.

It seems UPROPERTY is indeed the culprit here.

For me having changed UPROPERTY(BlueprintReadOnly) to UPROPERTY(EditDefaultsOnly) seemed to have worked for my Actor Component.

Before, whenever I would reference the Actor Component from a deriving Actor Blueprint, it would throw an error, saying the component is dereferenced e.g: Accessed None trying to read property MyComponent.

This is just speculation on my part, but it might be that setting UPROPERTY makes the component prone to garbage collection, especially when the component is mostly empty or has a few print calls.

more ▼

answered Feb 20 '19 at 10:53 AM

avatar image

26 1 4 5

avatar image Dune Feb 20 '19 at 01:05 PM

Not sure if fully related, but I found that using "VisibleAnywhere" in place of "EditAnywhere" will allow the component to show up in the blueprint editor.

avatar image Acds Mar 20 '19 at 04:44 PM

Thanks, both of you seem to be correct, marked it as answer since this seems to be the only way around it for the time being.

(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