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.