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"

Tarray[0] 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[0] it always equals null.

This is how i set it up:

PlayerCharacter.h:

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
             TSubclassOf<APickUpItem> WeaponTest;
 
     UFUNCTION()
         void AddItemToInventory();

PlayerCharacter.cpp:

 void APlayerCharacter::AddItemToInventory(){
     Inventory->inventoryItems.Add(Cast<APickUpItem>(WeaponTest));
 }
 
 void APlayerCharacter::OnToggleInventory()
 {
     int32 numb = Inventory->inventoryItems.Num();
     FString strng = FString::FromInt(numb);
     if (Inventory->inventoryItems[0] == Cast<APickUpItem>(WeaponTest)){
 //This returns true
         Inventory->holdInventoryItem(Inventory->inventoryItems[0]);
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Num Items In Inventory: " + strng));
     }
 }

Inventory.h

 void UInventory::holdInventoryItem(APickUpItem* item){
     
     if (item){
             //This returns false
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("item"));
      }
         
 }




Any help would be appreciated.

Thanks,

EDIT

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

 void APlayerCharacter::PickUp(){
         if (canLookAtItems){
             if (itemInView != NULL){
                 if (itemInView->IsA(APickUpItem::StaticClass())){
                     Inventory->addToInventory(Cast<APickUpItem>(itemInView));
                 }
             }
         }
     }

Which obviously goes to my Inventory class and does this:

     void UInventory::addToInventory(APickUpItem* item){
         if (item != NULL){
 //true
             inventoryItems.Add(item);
             if (inventoryItems[0] != NULL){
                     //false
             GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Add To Inventory" + inventoryItems[0]->Name.ToString()));
             }else{
                     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Do Not Add To Inventory"));
     
             }
         }
     }

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.

Product Version: Not Selected
Tags:
more ▼

asked Apr 18 '16 at 06:21 AM in C++ Programming

avatar image

joelComberiati
122 26 22 29

avatar image eXi Apr 18 '16 at 01:27 PM

Hey there,

could you add some more information about when these function are getting called? Do you, maybe, destroy the WeaponPickup at some point?

avatar image joelComberiati Apr 18 '16 at 02:15 PM

I add the items at the very beginning in the player constructor, and then the hold inventory items is called when I press a key.Actually I never do anything with the item because it is a test run in orDer to islate the variables. I was wond r ring that originally so I created the item so that I would know I am not adding something that isn't there. I was wondering if It had something to do with the casting?

avatar image PrinfD Apr 20 '16 at 08:43 PM

I don't see any issues with the code that you provided. Do you maybe still call your void APlayerCharacter::AddItemToInventory() function anywhere? Because, as I pointed out, this will actually add a nullpointer to your TArray. And since you are only checking the first index in your addToInventory() function, that might cause the issue.

avatar image joelComberiati Apr 20 '16 at 10:10 PM

No, completely removed it. You are right though, when I insert it at 0 it works. Thanks for the help, ill have to do a lot of searching to find out where this initial element is coming from.

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

2 answers: sort voted first

Hey,

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.

more ▼

answered Apr 18 '16 at 03:22 PM

avatar image

PrinfD
138 4 3 8

avatar image joelComberiati Apr 20 '16 at 04:54 PM

I just tried what you suggested, and then came up with this little piece:

 void UInventory::addToInventory(APickUpItem* item){
         if (item != NULL){
             //item->Destroy();
             inventoryItems.Add(item);
             if (inventoryItems[0] != NULL){
                 GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Add To Inventory" + inventoryItems[0]->Name.ToString()));
             }else{
                 GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Do Not Add To Inventory"));
     
             }
         }
     }

Obviously says Do Not Add to Inventory on the screen. So maybe it is not a casting error.

avatar image joelComberiati Apr 20 '16 at 04:56 PM

For Some reason it wont let me put the code into proper formatting, so please, pretend it is.

avatar image PrinfD Apr 20 '16 at 06:11 PM

I don't notice anything wrong about this. Did you maybe add something else to your TArray before you called this code? So that your first index could be a nullptr? Otherwise I think you will actually need to provide some more information about how and with what values you call this function.

avatar image joelComberiati Apr 20 '16 at 07:49 PM

Did some editing to the original question, please take a look if you got the time

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

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.

if (Inventory->inventoryItems[0] == Cast(WeaponTest))

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 (:

Cheers!

more ▼

answered Apr 18 '16 at 02:20 PM

avatar image

eXi
6.8k 333 185 470

(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