Catching event without ever knowing instance that called it c++

Hi everyone,

I have a hopefully quite simple question, but can’t find an answer.

Let’s say I have an Actor “Caller” that calls an event and I have an Actor “Receiver” that can catch this event, but doesn’t know the instance of “Caller” that has called the event, and notices when every instance of “Caller” calls the event.

From what I have understood, for binding a delegate the instance of “Receiver” needs to “know” the instance of the “Caller” to bind it.

Hi there! You can use instance delegates, but you can also use static delegates. I think this can solve your issue ) This is very comfortable to operate with delegate without the need of knowing if any instances exist )

And do you have something where I could look static delegates up, I only saw people saying that static delegates aren’t possible.

Ok, watched your example. I’m not that familiar with delegates so I tell you how I understand it.

IMyInterface() { MyDelegate.AddRaw(this, &IMyInterface::CallMe); }

“this” is the class that implements this interface so it binds it to the delegate and calls the overridden function CallMe. Can I broadcast it from anywhere via:

IMyInterface::MyDelegate.Broadcast();

Does saving mean that the “bindings” are saved, and is it much slower?

Ok, seems like that is what i need, will have a look into it. Thanks!

Do you now how good the performance is for this on large numbers? Like is there a significant difference from looping over 500 actors and calling the function on them or doing it with delegates?

Here was an example https://answers.unrealengine.com/questions/1002614/c-broadcast-interface-function.html.

As for performance - delegate broadcast just iterates through all subscribers and call method pointers on each. Dynamic delegates can be saved (this is their main trick) and of course they are slower, because use some reflection. That cant be quicker than C++ raw function pointers. So you should’t use them if that is really often call.

I wanted to test it and think i copied everything from you except the names, but I get an error from this code:

#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "TestInterface.generated.h"

DECLARE_MULTICAST_DELEGATE(FTestDelegate);

UINTERFACE(MinimalAPI)
class UTestInterface : public UInterface
{
	GENERATED_BODY()
};
class RTS_API ITestInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:

	static FTestDelegate TestDelegate;

	ITestInterface() {
		TestDelegate.AddRaw(this, &ITestInterface::InterfaceFunction);
	}
	UFUNCTION()
		virtual void InterfaceFunction() {}
};

I get the error LNK2001: Cant resolve symbol:

“public: static class TMulticastDelegate ITestinterface::TestDelegate”

EDIT:
FDelegate ITestInterface::TestDelegate;

in the .cpp does the trick, don’t know why :D.

Thanks for your help!

C++ require static values to be initialiazed somewhere. So dont forget to add this line in your CPP file:

FTestDelegate ITestInterface::TestDelegate;