Download

Blueprint Based Item System?

So hey, to keep things short

Im sort of stumped, Trying to create a basic item system ( Pick up physical items in the world that get added to the player inventory etc )

i got the basics working fine, I can pick up items from the world and they are stored in the player inventory, Now the MAIN issue comes when it comes to calling functions in the items from the
inventory array, Im trying to use a blueprint to handle item actions ( Using,Dropping etc ) so i wouldnt need to manage it trough c++, So for this i created a event that i can use in my item blueprint
thats called when said item is used

IF i dont call Destroy() on the physical item the event works fine, if i destroy the item in the world and call the event from the item in the inventory array the blueprint doesnt work… Here is the code im using to pick up the items and add them to the world

ACharacter Header



#pragma once

#include "ProojectCharacter.generated.h"

class AProojectItem;

UCLASS(config=Game)
class AProojectCharacter : public ACharacter
{
	GENERATED_UCLASS_BODY()

	/** items inventory */
	UPROPERTY(EditAnywhere, Category = Inventory)
		TArray<class AProojectItem*> ItemInventory;

	void PickUpItem(AProojectItem* Item);



ACharacter.cpp



void AProojectCharacter::PickUpItem(AProojectItem* Item)
{
	if (Item)
	{
		Inventory.Add(Item);
		Item->BeginDestroy(); // Item is in inventory now
                ItemInventory[0]->Used(); // Only works if i dont call destroy
	}
}



I know i can instead of destroying the class just hide its graphical components but what if i have 999 items in the world ( just an example number) Even if their visible elements such as static mesh and collision are disabled i still have all of these instances in the world it seems kinda wrong to just leave them there instead of keeping them only in the Inventory array

When you call Inventory.Add(Item), you are just adding a reference to that item to the array, there is still just one item. So when you destroy it, that item is dead, so you can no longer use it. If you want to be able to call functions on your inventory items, they will need to exist, you will need to carry them around with you. Another option is to just store the class of the item instead of an actual instance. That would be more efficient, but you would have to spawn an instance of the class to do something with it.

By the way, I think you want to use Destroy() and not BeginDestroy()!

Ah that makes sense guess ill just store the class instead and spawn it when needed ,Thanks!