Subscribing to event

Thank you very much! :slight_smile:

Multi-Cast delegates do work just the way events do. But from your link, I could find another link to events, and now it works flawlessly.

Thank you again!

edit –

In case anyone else got stuck like me with c++ events, and found this post, here’s how I’ve done it:

Class with event:



**.h:**
DECLARE_EVENT_OneParam(ATemplateCharacter, QPressed, FString)

UCLASS()
class ATemplateCharacter : public ACharacter
{
	...

	/*Skill held by the character*/
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Skill)
		class UTemplateBaseSkill* QSkill;

	/*Event when Q key is pressed*/
		QPressed FirstEvent;

	/*Function that will fire event*/
	UFUNCTION(BlueprintCallable, Category = Skill)
		void Fire();
};

**.cpp:**
#include "BaseSkill.h"

ATemplateCharacter::ATemplateCharacter(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	// Create Skill and call it's subscription function, so that it can subscribe to all the necessary events (the skill gets pointer to owner) 
	QSkill = NewNamedObject<UTemplateBaseSkill>(this, TEXT("QSkill"));
	QSkill->Subscribe(this);
}

void ATemplateCharacter::Fire()
{
	FirstEvent.Broadcast("jack");
}


Class subscribing to event:



**.h:**
	//Using skill
	UFUNCTION(BlueprintCallable, Category = Skill)
		void Use(FString Caller);

	// function for subscribing
	UFUNCTION(BlueprintCallable, Category = Skill)
		void Subscribe(ATemplateCharacter* MyOwner);

**.cpp:**
void UTemplateBaseSkill::Subscribe(class ATemplateCharacter* MyOwner)
{
	MyOwner->FirstEvent.AddUObject<UTemplateBaseSkill>(this, &UTemplateBaseSkill::Use);   //this is the worst :D
}

void UTemplateBaseSkill::Use(FString Caller)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *(caller));
}


2 Likes