Tarray returns null when elements are inside
Hello, So I have a Tarray for my inventory items of type APickUpItem. I have added, emplaced, inserted, and setNum() and then did the same. Basically I did everything I could think of to add items to the list. I have checked the .Num() and it returns correctly as far as I can tell. The problem occurs when I reference an item within the list like so: itemInventory it always equals null.
This is how i set it up:
Any help would be appreciated.
Okay, So using PrinfD's suggestion i added my blueprint to the level manually by dragging and dropping it in. Blueprint is a subclass of HoldableItem which is a subclass of PickUpItem. The I then use a line trace to check what item is in view which then stored in a Actor Variable. Then of course in my player class
Which obviously goes to my Inventory class and does this:
On another project I did something very similar... but now it does not seem to work
EDIT Okay, So I did even more tests, the casting is not the issues, whatever is causing the object to be NULL is after or during the addition to the array.
To me it looks like your issue is with the WeaponTest variable. It's of the type TSublcassOf (Typesafe UClass, just holds information about a class). Now you are trying to actually add this variable to the TArray, which can't work because it's not actually a variable of the type APickUpItem, just a reference to a subclass of APickUpItem.
So what you would want to do is use your WeaponTest variable to spawn an actor of that class in the world and then add the spawned actor to your TArray.
Hope fixes the issue.
answered Apr 18 '16 at 03:22 PM
Are you sure that the WeaponTest is of Type APickupItem ?
Because if it's not, the Cast will return NULL and you just add a nullptr to your Array.
This will be true then, because if you added a nullptr to the Array and then comparing it to the Cast result (again), you have
if(nullptr == nullptr), which will be true. That's why the IF is true, but you still have NULL for the actual item.
Make sure the WeaponTest is of Type APickupItem or at least a Children of it.
That should fix your problem (:
Follow this question
Once you sign in you will be able to subscribe for any updates here