How to code events in c++?

Doc from here don’t explain all the process (and don’t compile):

doc there is different :

and code from unreal is different from both :

/** Called when selected level list changes. */
DECLARE_EVENT( UWorld, FOnSelectedLevelsChangedEvent);

/** Broadcasts whenever selected level list changes. */
FOnSelectedLevelsChangedEvent				SelectedLevelsChangedEvent;

So what’s the right way to code events from start to end ?


Hey w77, where are you declaring this- is it in an interface?

Hey w77

I just tested an event declaration in an interface;

DECLARE_EVENT_OneParam( UActionManager, FEventSignature, AActor* )

in the header;

static FEventSignature MyTestEvent;

In the cpp;

FEventSignature IActionManagerInterface::MyTestEvent = {};

It compiles fine. So then all I’d need to do is bind the event to a function in the UActionManager, which is the class I’ve assigned as the owning type.

MyTestEvent.BindUObject(this, &UActionManager::SomeFunction);

Let me know if that works! If the class you’re declaring it in isnt abstract, then you probably wouldn’t want it to be static.

If it isn’t static then don’t put anything in the cpp. you shouldn’t need to initialize it…

no, it’s a class event.

without static :
error C2761: ‘FDisableEvent UGeneratorComponent::DisableEvent’ : member function redeclaration not allowed

	FDisableEvent DisableEvent;


FDisableEvent UGeneratorComponent::DisableEvent = {};

I don’t understand why set the event public… any class anywhere can fire broadcast.

you can make it private in the header. The DECLARE_EVENT is above the class definition though. Between the #includes and the class definition…