x

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"

Initialised UObject always returns NULL?

Hello everyone,

What I am trying to do is to attach a UObject to my item class that is going to hold all the configuration for the class, the definition looks like this:

     UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = Config)
         UItemData* ItemConfig;

And in the constructor I initialise it like this:

     ItemConfig = ObjectInitializer.CreateDefaultSubobject<UItemData>(this, TEXT("ItemConfig"));
     ItemConfig->Name = TEXT("Item");
     ItemConfig->Description = TEXT("Empty");
     ItemConfig->Weight = 1.f;
     ItemConfig->bCanStack = false;
     ItemConfig->MaxStackCount = 0.f;
     ItemConfig->Image = nullptr;


But everytime I try to use the ItemConfig outside of the item class it is always NULL, this is the code I am using:

     AActor* ActorInView = GetActorInView();
 
     if (ActorInView)
     {
         AItem* Item = Cast<AItem>(ActorInView);
 
         if (ActorInView->IsA(AItem::StaticClass()) && Item)
         {
             if ((CurrentInventory->WeightCounter + Item->ItemConfig->Weight) > CurrentInventory->MaxWeight)
             {
                 GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Item is too heavy!!"));
                 return;
             }
 
             PickUpItem(Item);
         }
     }


The last code shown always crashes the project because of the "Item->ItemConfig->Weight" part is always NULL.

Any help is appreciated!

Product Version: UE 4.11
Tags:
more ▼

asked May 08 '16 at 10:23 AM in C++ Programming

avatar image

onathamarat1
10 3 4 9

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

2 answers: sort voted first

Hi onathmarat1,

I had trouble too with a similar issue. I tried the NewObject function back then with outer being some actor that is spawned. My guess was and still is that the garabage collector believes it is no longer referenced although it is. So instead try to init is with NewObject. Some other post I read the other day recommended making the ItemConfig an actor. Though I would rather recommend this:

I ended up creating an actor component which held the data of ItemConfig and I attached the component to my actor that needed the ItemConfig. That works and was also recommended to me at that time.

Just beside the point, it might be a better design choice to place the data in an actor component anyways because components can be attached dynamically. This would allow more a more flexibile and maintable design. That depends on your project though and is just a thought I wanted to provoke.

Cheers, Univise

more ▼

answered May 08 '16 at 10:47 AM

avatar image

Univise
201 9 10 22

avatar image onathamarat1 May 08 '16 at 11:12 AM

Quite strange :/ the problem still persists even though I have tried NewObject and changed it to UActorComponent. The reason I am using a separate object is because I am going to use the class with the inventory system I am going to implement. Just thought It would make more sense that way.

avatar image Univise May 08 '16 at 11:37 AM

You must attach the actor component to your actor.

avatar image onathamarat1 May 08 '16 at 02:03 PM

I am not quite sure what you mean by that, I can't find the "AttachTo" function that all the UObject derivatives have. Mind showing what you mean?

avatar image Univise May 08 '16 at 04:20 PM

Let 'Comp' be the the component's variable name and 'Owner' the actor's variable name which you want to be attached to. Simply call Owner->AddOwnedComponent(Comp). See also: https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/AddOwnedComponent/index.html

avatar image roomon1 May 09 '16 at 07:33 PM

Hey Universe,

Just wanted to point out the alternatives I suggested in my answer. They may be helpful for your case (or your future work) too.

Cheers,

-Roomon1

avatar image onathamarat1 May 10 '16 at 05:20 PM

I have come to the decision that UObject is not necessery, struct just works fine plus it's not as heavy. Thank you all for the help! :)

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

Hi onathamarat1,

I'm inclined to suspect Universe is right -- it looks like this is a case of garbage collection removing your object when you are still using it.

As an alternative to creating an Actor-based or component-based version of your data structure, you might consider either using a hard (non-pointer) reference to the structure (if possible), or adding the object to the root set. According to the documentation on the engine's object handling, either of these ought to prevent the object from being cleaned up by the garbage collection. I found adding the object to the root set to work perfectly in some work I was doing, though depending on the lifetime of your actors, you may need to clean up the data structure yourself when destroying the actors.

I hope this helps!

-Roomon1

more ▼

answered May 09 '16 at 07:32 PM

avatar image

roomon1
191 4 5 11

(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