Fill a TArray with UObject

Hi everyone !

I’m pretty new to UE4 C++. I’m on UE5. I’m coding a basic inventory system. I made a UInventorySlot (UObject) and a UInventoryComponent (UActorComponent). I just want to fill a TArray variable in the UInventoryComponent to init the inventory with empty slot. But it doesn’t work. At the end of the loop to init it, the TArray is full and correct, but when I try to access it via a function for example, the array is empty (num==0).

In the UInventoryComponent BeginPlay, I do this :

The TArray declaration in UInventoryComponent header :
image

The UInventorySlot class :
image

I have checked every forums, videos and documentations, I don’t find anything about this problem. Any idea ?

Hello, could you try looking at the array from the Editor?

There doesn’t seem to be anything immediately wrong with your code.

Maybe you could try replacing this with GetTransientPackage() on your NewObject line?

I replaced, I can see the slots in the editor, but Visual Studio continue to say it’s empty. What happen ?

image

Edit : I displayed the number of element in the c++ to be sure.
image

You may have a blueprint that already checkpointed the empty state.
Check if there’s a “revert to default” yellow arrow in the inventory property in a blueprint instance.

Or perhaps the warning comes from a different instance of an class, that hasn’t had the inventory added to it?

Did you mean this ?
image

If I press it, it just empty the array.

I spawn 2 players characters, both have the inventory full of slots on the blueprint details, but both have 0 element when you try to access to it.

Have you tried to init your array with TArray.Init() to see if that works? I know thats not a solution to the problem but it might help you narrow down the issue.


I tried this, same result except the array is full with the same slot (as intended) on the details, still num = 0 in c++.

There are really no setting in the UProperty or something like that ? It should works like this ?

Okay. What happens if you inspect the player objects / blueprints at runtime? Are the inventory slots still present?

How do you spawn your playerchars? If you havn’t tried already, try to drag one into the map and set that specific playerchars inventory to something and inspect it at runtime.

It’s always at runtime that I test it. I init it at BeginPlay. In the blueprint details, there are slots, in the c++ code, it’s empty.

I spawned them with simply two PlayerStart. I tried to drag one, same result, at runtime, the blueprint have slots, the code c++ not.

Edit : I’m on UE5 if it have any importance.

Okay I have more informations :
I did an initialization function and called it by an input, with the exact same loop. It worked.

So something happen after the beginplay and erase the pointers… But what ?

Edit :
I start to have something, I tested if the component was initialized during beginplayimage
And it isn’t. So I think the component initialization is done after beginplay and erase my own… Just need to know how to be sure BeginPlay is launch after init, or a way to detect when the init is done to launch my own.

Edit 2 :
No, the component init is only called if you want it and it’s before beginplay anyway… Seriously, there is nothing after beginplay… What the hell…

Edit 3 :
Nevermind, the problem seems to come from the Character class owning the component. I tested on a clean one, it works.