C++ BROADCAST INTERFACE FUNCTION???

Hello! I’ve found my self in need to broadcast one of my interface functions like a multicast delegate. I need an interface to call some of my functions directly but I also need to broadcast the same function. I’m currently using TObjectIterator to iterate through all objects and to call the interface function if they implement it. But this doesn’t seem like the most efficient way to do this. Is there a way I can implement some kind of broadcast function? Perhaps somehow bind all of the functions to a delegate and broadcast it from there? Please respond with reasonable and efficient answers. Thanks!

So you always call this method on all actors that implements this interface?

I mean ALWAYS ALL? Or sometimes only part of them?

I don’t need to ALWAYS call this method, sometimes I need to call an interface function directly and sometimes I need to “broadcast” the same interface function. And not just actors, all UObjects that implements the interface. I’m looking for a better method than iterating through every UObject checking if it implements the interface and calling the function. It doesn’t feel efficient for performance reasons. Thanks for your reply!

Oh, all then. All the UObjects, actors and widgets included.

Ok. At the moment not ideal solution, but you can try it:

  • Declare delegate in your UInterface header
  • Make public static variable of this delegate type in IInterface body
  • Create public CTOR in IInterface, use AddRaw to bind static delegate to method of this IInterface instance
  • Call static variable Broadcast anytime you need

However, you cant define DCTOR because it is already defined in generated.h. I think there should be some event before removing object by GC, maybe it can be used to unsubscribe.

But how would I get a reference to the interface instance? When I use “this” it says the types are unrelated.

#pragma once

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

DECLARE_MULTICAST_DELEGATE(FMyDelegate);

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyInterface : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class MYPROJECT_API IMyInterface
{
	GENERATED_BODY()


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

	static FMyDelegate MyDelegate;

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

	UFUNCTION()
		virtual void CallMe() {}
};

How can I do this with a delegate that has parameters? It says I can’t convert argument 2 to blah

HEADER

#pragma once

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

 UENUM()
 enum EMyEnum
 {
      FirstChoice,
      SecondChoice
 }

DECLARE_MULTICAST_DELEGATE_OneParam(FMyDelegate, const TEnumAsByte<EMyEnum>);

UINTERFACE(MinimalAPI)
class UMyInterface : public UInterface
{
     GENERATED_BODY()
};

class MYPROJECT_API IMyInterface
{
     GENERATED_BODY()

public:

     IMyInterface();

     static FMyDelegate MyDelegate;

     virtual void MyFunction(const EMyEnum MyEnum);
}

CPP:

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

IMyInterface::MyFunction(const EMyEnum MyEnum)
{
}

Hello! Just change this

virtual void MyFunction(const EMyEnum MyEnum);

with this

virtual void MyFunction(const TEnumAsByte<EMyEnum> MyEnum);

Keep methods signatures fully matched with and delegate defenition. Also you dont need to always use TEnumAsByte, in fact it is only for using UPROPERTY. One more thing if you do a const param, it is more valuable to pass it by ref or by ptr

Oh my bad. Little typo. I still get the error “cannot convert argument 2”. Still, how can I use add raw with a delegate that has parameters?

Can you share full text of error?

Ok I’ve fixed most of my errors. I was using a blueprint native event and I forgot to add TEnumAsByte<> in the actual function itself. Anyway now I get this error:

MyInterface.gen.cpp(93): error C2511:
‘void IMyInterface::MyFunction(const
EMyEnum)’: overloaded member function
not found in ‘IMyInterface’

The header and the cpp file have the same function signature for MyFunction, which is IMyInterface::MyFunction(const TEnumAsByte<EMyEnum> MyEnum) but the generated.h file removes the TEnumAsByte<>.

You shouldn’t use TEnumAsByte in func params. The idea of TEnumAsByte is for storing enum in UPROPERTY, in all other places you re free to use enum itself without any wrapping

Yeah sorry, the previous comment I posted was for the one you said “please show full error”. For some reason answerhub sometimes has a delay before posting something so that comment was from days ago. Anyways, I’ve removed TEnumAsByte from everything in my project except when in containers like TArrays, TMaps, and in UProperties. Now I get a crash saying “Use Execute_MyFunction() instead”.

You still with me?

Hi there! Happy NY And MC. Can you show full code that give you this error (interface code)?

Header:

#pragma once

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

DECLARE_MULTICAST_DELEGATE_OneParam(FMyDelegate, const EMyEnum MyEnum);

UCLASS(Blueprintable, BlueprintType, MinimalAPI)
class UMyInterface : public UInterface
{
     GENERATED_BODY()
};

class MYPROJECT_API IMyInterface
{
     GENERATED_BODY()

private:

     IMyInterface();

     static FMyDelegate MyDelegate;

public:

     UFUNCTION(BlueprintNativeEvent)
     void MyFunction(const EMyEnum);
     virtual void MyFunction_Implementation(const EMyEnum);
};


CPP:

#include "Interfaces/MyInterface.h"

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

void MyFunction_Implementation(const EMyEnum)
{
}

try this:

.H

#pragma once

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

UENUM()
enum class EMyEnum: uint8 {
	One,
	Two,
	Three
};

DECLARE_MULTICAST_DELEGATE_OneParam(FMyDelegate, const EMyEnum);

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyInterface : public UInterface
{
	GENERATED_BODY()
};

class MYPROJECT_API IMyInterface
{
	GENERATED_BODY()

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

	IMyInterface();

	UFUNCTION(BlueprintNativeEvent)
		void MyFunction(const EMyEnum myEnum);

	virtual void MyFunction_Implementation(const EMyEnum) {}
};

.CPP

#include "MyInterface.h"

// Add default functionality here for any IMyInterface functions that are not pure virtual.

FMyDelegate IMyInterface::MyDelegate;

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

I don’t think you understand my problem. The problem isn’t the enum definition. It’s the way you’re suggesting to call the functions. Unreal crashes if I call the interface directly and it suggests that I should use function with the Execute_ prefix instead. I’m starting to think that binding the interface functions to a delegate was a bad idea. Isn’t there a way I could efficiently iterate through all the objects with the interface? Not through every object to check if it implements the interface and executing it?