Download

C++ Events

Hi everyone! I’m not used to ask for things on forums but this is kinda driving me mad so I hope somebody here can explain me what I’m doing wrong.

My goal is apparently the most simple one: declaring a basic event with 1 bool parameter which will be used to notify when an asset load starts or ends.

Documentation about events is pretty much short, and I think I followed it correctly. I reduced the class to the bare minimum (and it’s in an almost empty project), here it is:



#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "RSNCustomCharacter.generated.h"

DECLARE_EVENT_OneParam(ARSNCustomCharacter, FLoadEvent, bool)

UCLASS()
class RSN_API ARSNCustomCharacter : public ACharacter
{
    GENERATED_BODY()
    public:
    UPROPERTY(BlueprintAssignable)
    FLoadEvent OnLoadStatusChanged;
};


Well, it doesn’t compile, telling me that FLoadEvent should be a UCLASS, USTRUCT or UENUM on the first line after #includes… The DECLARE_EVENT_OneParam macro should translate to



class FLoadEvent : public TMulticastDelegate<bool>
{
    friend class ARSNCustomCharacter;
}


so I can’t figure out what’s wrong with such a simple class declaration in such a simple header file…

Any concrete help would be appreciated, please don’t link the documentation. I’ve already read it so if the answer is there I can’t see it at all… Can’t believe I’m stuck on such a simple thing… Events are too much important to give so many problems!

The problem is the



UPROPERTY(BlueprintAssignable)


As it mentions, and as you pointed out by posting the TMulticastDelegate portion - an Event is not a UCLASS/USTRUCT/UENUM, so they can’t be a UPROPERTY or made Blueprint assignable like that.

You may also want to pull the declare into the class definition itself (other engine code seems to follow this paradigm):




#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "RSNCustomCharacter.generated.h"

UCLASS()
class RSN_API ARSNCustomCharacter : public ACharacter
{
   GENERATED_BODY()
   public:
   DECLARE_EVENT_OneParam(ARSNCustomCharacter, FLoadEvent, bool);
   FLoadEvent OnLoadStatusChanged;
};


As @ExtraLifeMatt said. If you want to have events in blueprint, you can use the DECLARE_DYNAMIC_MULTICAST_DELEGATE macro. In your case, the _OneParam version to be more precise.

Thank the both of you for answering.

To sum up, @ExtraLifeMatt is telling me that events (DECLARE_EVENT) cannot be exposed to blueprints, since I cannot ad a UPROPRTY to a non UCLASS, USTRUCT or UENUM, and @STRiFE.x tells me that if I wat to expose them to blueprint, which I do, I should use multicast delegates (DECLARE_DYNAMIC_MULTICAST_DELEGATE).

I made it work with multicast delegates, that’s better than nothing. BTW multicast delegates documentation is confusing, besides unescaped html entities there’s no mention to requred parameters in the macros table, and they differ from singe cast apparently since the single cast macros table doesn’t specify the parameter name on the docs.

What I don’t get now is: is there no way to expose a delegate to blueprints but still preventing it to be invoked and manupilated outside a given class?

What is this for?


UPROPERTY(SealedEvent)

@gamepainters It’s only valid for UFUNCTION and UDELEGATE macros. Copied from ObjectMacros.h:



/// This function is sealed and cannot be overridden in subclasses.
/// It is only a valid keyword for events; declare other methods as static or final to indicate that they are sealed.
SealedEvent,