Hey there,
it happens that i did this 2 days ago. Since i want code examples in the docs, i will give you a small code example on how to setup a delegate
to be used in Blueprints and C++.
First you need to add your Delegate through the macro in your classes header file:
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTestDelegate);
“FTestDelegate” represents the Typename of your upcomming delegates.
You can use Params by adding _OneParam or _TwoParams and so on at the end.
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FTestDelegate, PARAMTYPE, PARAMNAME);
Example:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FTestDelegate, float, Damage);
You place this outside of your class declaration(example):
#pragma once
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"
/**
*
*/
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTestDelegate);
UCLASS()
class SOCKETTEST_API AMyPlayerController : public APlayerController
{
GENERATED_UCLASS_BODY()
public:
virtual void Tick(float DeltaTime) override;
virtual void BeginPlay() override;
void TestFunction();
UPROPERTY(BlueprintAssignable, Category = "Test")
FTestDelegate OnTestDelegate;
};
Now you should have spotted the UPROPERTY at the bottom of my example.
This is the part you add a Delegate (here called “OnTestDelegate”).
The UPROPERTY “BlueprintAssignable” give you the possibility to bind/assign
this function to a custom event in your BP Child class.
You should be able to bind a delegate in C++ AND Blueprint and call both
bound functions/events through c++.
UPROPERTY(BlueprintAssignable, Category = "Test")
FTestDelegate OnTestDelegate;
If you want to add a function to it, you need to call “AddDynamic” on the delegate.
The editor won’t show you this and maybe shows an error. Just try to compile anyway…
Here is an example for the normal, non param delegate from above, inside your CPP File:
OnTestDelegate.AddDynamic(this, &AMyPlayerController::TestFunction);
If you now want to call your delegate inside your code do this at the desired position:
OnTestDelegate.Broadcast();
I hope this helps (: