Download

TMap<int32, Actor> - Crash on .add

Hello everyone,

i have a huge problem and no idea where to find help, maybe i just don’t get it. Well sometimes rubberducking helps, but if you have an idea feel free to post :wink:

PlayerInventor.h


TMap<int32, AUsableActor*> Inv;

so i pick up the item (getting the item reference with a raytrace), then i call from PlayerCharacter the Inventory function



bool APlayerInventory::AddItem(AUsableActor* Item)
{
	if (Item) {
	if (Item->IsValidLowLevel())
	{
		 
		Inv.Add(1, Item);

		return true;
	}
	}
	return false;
}

Following the call stack, it crashes @


 	
UE4Editor-ProjektMacijo-Win64-DebugGame.dll!TSparseArray<TSetElement<TPair<int,AUsableActor * __ptr64> >,TSparseArrayAllocator<FDefaultAllocator,FDefaultBitArrayAllocator> >::AddUninitialized() Line 87	C++
	
UE4Editor-ProjektMacijo-Win64-DebugGame.dll!TSet<TPair<int,AUsableActor * __ptr64>,TDefaultMapKeyFuncs<int,AUsableActor * __ptr64,0>,FDefaultSetAllocator>::Emplace<TPairInitializer<int && __ptr64,AUsableActor * __ptr64 const & __ptr64> (TPairInitializer<int &&,AUsableActor * const &> && Args, bool * bIsAlreadyInSetPtr) Line 421	C++
 UE4Editor-ProjektMacijo-Win64-DebugGame.dll!TMapBase<int,AUsableActor * __ptr64,FDefaultSetAllocator,TDefaultMapKeyFuncs<int,AUsableActor * __ptr64,0> >::Emplace<int,AUsableActor * __ptr64 const & __ptr64>(int && InKey, AUsableActor * const & InValue) Line 349	C++
 UE4Editor-ProjektMacijo-Win64-DebugGame.dll!APlayerInventory::AddItem(AUsableActor * Item) Line 95	C++ 


And shows me the AddUninitialized() function (in the Set.h).

So there is some point with the allocation, but i have no ****ing idea what exactly :frowning:

Please help me!

Have a nice evening / night

Greetz Mopfi

Why is player inventory an actor? Should be UClass or UStruct based, imo. Anyway.

You need to check that your “inventory” object actually exist when you call AddItem.

Because if you do something like that:



APlayerInventory* inventory;


It will not be automatically created for you.

Also, you might want to declare your map as UPROPERTY.

ShooterGame uses actors as items. Probably for easier replication.

Items. Not item inventory.