Making a copy of a delegate variable

Hi. I am making an inventory system for the game i’m currently making and I’m wondering if I could make another instance of the delegate FOnItemUse variable of a certain item class for the inventory component to use.

If that is possible, would I have to worry that it might be collected by ue4’s GC when for example moving to another level and would I still retain the functions bind in the original delegate that i duplicated or i am looking at this problem at the wrong angle?

I am so sorry, I am a beginner in unreal engine. I recently switched to unreal from unity. I am still not sure if it is even possible to do this at c++ but from what i know is that classes in c++ are structs under the hood with pointer variable for the functions so I wondered if somehow i can obtain or duplicate the delegates inside the class without spawning them just to run a delegate.

thank you very much.

here is my the top of my inventory header file

USTRUCT(BlueprintType)
struct FInventorySlot
{
	GENERATED_USTRUCT_BODY()

public:

	FInventorySlot()
	{
	}

	TSubclassOf<AItem> ItemType;
    FOnItemUse * OnUseItemFunc;
	int32 Count = 0;
};

Here is my item class

DECLARE_DYNAMIC_DELEGATE_OneParam(FOnItemUse, class ACharacter*, User);

UCLASS()
class PRACTICE_API AItem : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AItem();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(BlueprintAssignable, Category = "Item Events")
		FOnItemUse Item_OnUse;
    
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Setup")
    int32 MaxStackCount = 3;


};

I don’t really understand what you trying to do, i can examplin you how variables work in C++

In C and C++ absolutely all varables are data in memory which structure depends on type,… this includes pointer, in reality it just integer variable containing memory address, and it does not care if there anything in there, this is something you should keep track of. If your pointer points to varables in object, object needs to be in there other wise you pointer points to random data on unallocated memory, in other words it become invalid pointer. Again it just integer, so there no magic behind it really, you need to null (zero) the pointer if you know object don’t exist, or else you got system does that for you and UE4 has something like that in reflection system when you use UPROPERTY, it will null pointers it knows about automatically. There also smart pointer system, which helps management

Size of type is always static, any dynamic size require dynamic allocation, for example FString is actully array of TCHARs (Whcih has fixed size), it dynamically allocate space with fixed space for them elsewhere and just keep pointer to it, when you resize FString wither reuse allocated space or decides to reallocate it elsewhere with new size, you can actully control this process with Reserve and Shrink functions. This static nature of C++ is the reason why C++ don’t have proper string support and requires proper implementation of it on top of it (thats why UE4 got FString)

Statically declared variables are allocated straight away inside the scope (if you dont know scope is { } ) they are delcered, global varbales (global scope) are allocated when you turn on application, local varbales inside functions are allocated when score they are in is executed, compilers add instructions for that when scope starts, most of them actually use CPU registers to store them if there life time is short for optimization and they never formally exist in memory. Varbales in the class or structure, only describes the structure of them and are made when you declare that class or structure, compiler tracks the memory address offsets, so it know which part of object or structure variable is specific variable.

When you run function on top of object and structure (regardless if from pointer or not), the compiler sets “this” pointer (usally keep it in CPU register) and when you operate object varbales it using “this” pointer to know which one to set in memory. Or else it static function, then object only acts as a namespace.

You might be confused with → and . operators and why pointer need ot use → one, . operator calls function on the variable it self while → calls function on object that varables references to. So when you use . on pointer you actually attempting to call function on pointer it self which don’t have any functions. You may wonder what is sense of having this sperated when only pointers use that, but considering in C++ you can overload → operator behavior you can make a structure that refrences to something and you cna make → operator to let developers make call direly referenced objects to it while operate structure with . operator. UE4 use that for smart pointer support that i link docs about it above.

Fucntion can be also pointed and executed from the pointers, the pointer in that case point to address of function instructions. Thats why UE4 use for delegates, it’s simply function pointer container

https://www.learncpp.com/cpp-tutorial/78-function-pointers/

So you not “spawning” delegate, treat it like a function pointer container

So that delegate pointer will point to delegate in AItem, whatever it is valid depends of ot still exists in memory, if not it will be invalid. So you need to clear it (set to nullptr… or 0 ;p)when AItem it pointing to is deleted (yes this include level change) . No body gonna do that for you as you don’t have UPORPERTY on that pointer and reflection system only supports managing UObject pointers.

If you want to call function without spawning object it self then make static function and bind it to delegate (you can do this with BindStatic or AddStatic https://docs.unrealengine.com/en-US/API/Runtime/Core/Delegates/TBaseMulticastDelegate_void_Para-/AddStatic/index.html), ofcorse with static function you can not do anything to AItem. But i don’t know why you doing UClass pointer inventory with use function delegate ointer, if item exist in inventory it should be spawned as object, if you want to keep information in Game Instance and reconstruct it on level start.

If you have problem to understand nature of C or C++, just remember that C and C++ it’s more of a description how to generate CPU instructions for compiler more then proper script programming language like you had with C#. Everything you write is compiled down to direct CPU instructions or just information that compiler will use to generate them (like data structures of object and structs, so compiler know what memory address offsets to use), so all behaviors are a bound to behavior of CPU itself, you dealing direly with data memory and addresses to it. Thats how low level it is.