I’m kind of a beginner when it comes to c++ and i’m trying to write a container like class in c++ that is able to store and execute delegate types.
The issue is that i want it to be generic so i want it to work with any sort of delegate(void, int32 parameter, vector parameter etc) without having to hard code them.
I also want this to be blueprintable so the idea would be that i have a node with a 3 pins, a container target pin(for adding it to that specific container), a name pin(this used for storing and executing, ideally you can give it any name) and a delegate pin where you connect stuff like the output delegate of a custom event.
Is the system i’m trying to achieve even possible? I’ve ran out of ideas of how to create a system like this
Maybe, maybe not. It’s hard to tell based on what you’ve described so far.
You should start by describing the actual problem that you’re trying to solve or build instead of focusing on how to execute the implementation that you’ve already come up with.
This is the type of Delegate container i’m trying to achieve but i can’t find a Delegate type that would accept any type of Delegate regardless of what parameters it has. Also when i do find something that “might” work it’s not accessible in blueprints.
//This type of hard coding is what i'm trying to avoid!
DECLARE_DYNAMIC_DELEGATE(FVoidDelegate);
DECLARE_DYNAMIC_DELEGATE_OneParam(FStringDelegate, const FString&, String);
DECLARE_DYNAMIC_DELEGATE_OneParam(FVectorDelegate, const FVector, Vector);
DECLARE_DYNAMIC_DELEGATE_TwoParams(FIntFloatDelegate, const int32, Int, const float, Float);
UCLASS(Blueprintable, BlueprintType)
class FIRST_GAME_API UDynamicDelegateContainer : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Dynamic Delegate")
void AddDelegate(FName DelegateName, UObject* TargetObject, /*FDynamicDelegateType DelegateReference*/);
//I want this function to be able to take in any type of Delegate not just the ones specified by the
//DECLARE_DELEGATE Macros. I also want it to be Blueprint accessible.
private:
UPROPERTY()
TMap<FName, /*FDynamicDelegateType*/> DelegateMap;
};
You will never really be able to declare a delegate that can take anything. At some point you have to have a concrete signature that both the broadcaster and receiver agree on in order to pass information back and forth.
You still aren’t really describing the problem that you’re trying to solve with this container. You’ve add more details to your description of the solution you’re trying to make work.
But I’m going to go out on a limb and guess you’re trying to implement some sort of event system that senders and receivers can bind to without having to know who’s on the other end of that connection. Here is a solution that I have recently published on my github. At the very least give the readme a look to see if it describes what you’re trying to do. The solution here is not to have delegates that can take anything, but delegates that the compiler can easily generate in a well defined way that is type safe and knowable from both side of the communication channel.
It’s possible if you “abuse” the C++ reflection system of Unreal. In fact, I’m working on a level scripting tool that is capable of binding to any dynamic delegate with any params, and then calling any UFunction on an actor without casting. I shared my notes that I made while trying to understand how delegates work: Here is how C++ delegate macros work internally
Keep in mind that this is not something that I’d recommend doing as a beginner.
Ah I see what you mean.
Yes my original problem is related to what you suggested. My idea was to have a subscriber-publisher type system where components can easily send data to each other in a decoupled way. Delegates was just one of the ways I imagined might working.
I know that at a certain point you have specify things but I wanted to come up with a solution where the hard coding is minimized and works well with blueprints. So at the end I’d have a system where components can easily communicate with each other and you can modify/extend effortlessly.
I have read through the readme it seems like an interesting solution I might check it out in practice.