Initialised UObject always returns NULL?

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

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!

Quite strange :confused: 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.

You must attach the actor component to your actor.

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?

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: AActor::AddOwnedComponent | Unreal Engine Documentation

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!

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,

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! :slight_smile: