Crash when acessing TArray of TSubclassOf

Hey ItsaMeTuni,

Can you show me the code you are using to access the TArray?

Thanks

I have a class that derives from UDataAsset that is called ItemsDB, and then I have a Data Asset blueprint that is derived from ItemsDB.h . I also have a class called Pickup that is the base class for all items.

ItemsDB.h has an array like this:

TArray<TSubclassOf<APickup>> pickupActors;

When I open the Data Asset and add a variable to the array I can set a blueprint derived from Pickup.h with no problems, but when I try to access this array through code (in a class called Inventory) or even just check if the array is not null I got a runtime error and UE4 crashes.

Thanks foin advance!

Here it is:

APickup* itemToDrop;
	itemToDrop = itemsDB->itemsDBArr[itemInstance.itemID].pickupActor.GetDefaultObject();

Thanks for your help!

Did oyu check the logs Saved/Logs in project directory?

Yes and I didn’t find anything useful for me in it. I’d paste it here (just the section of the error) but it is too big for a comment.

I think that you’re accessing a portion of the array that doesn’t exist, causing the crash.

You could try something like:

APickup* itemToDrop;
if( itemsDB->itemsDBArr.Num( ) >= itemsInstance.itemID )
{
	itemToDrop = itemsDB->itemsDBArr[itemInstance.itemID].pickupActor.GetDefaultObject();
}

Paste it to quastion by edit, remeber to use code formating on it so it wont be a mess

I solved the problem myself… It turns out that itemsDB was null. Thanks to everyone that tried to help me!