How can I communicate between objects that doesn't have anything in common without coupling them?

Are delegates the answer?
Can I have an implementation example?

1 Like

Hi, it all depends on the situation. But to use delegates you need to have a reference to the owner of a the delegate (so I assume that it’s something that you called coupling).

In most cases interfaces are the answer. For example such functionalities as interacting with objects, unlocking something, open/close, are usually done with interfaces.
This is link to the documentation: Interfaces in Unreal Engine but it’s very common tool and you can find many tutorials online.

To let you briefly understand the idea, interfaces are functions that can be called on anything without casting - like sending a message. If an object has this interfaces implemented it will do it’s logic. If not it will just ignore the message. And what is great about interfaces is that they don’t do any hard references between objects.

1 Like

(post deleted by author)

1 Like

Hello, generally what you did is correct, just you don’t need to implement the interface in ATimeHandler if you don’t want it to react to this interface call.

Only the object that is actually “reacting” the the interface needs to have it implemented - so the PlayerController in your example. This is the actual benefit of the interface that anyone can send the message, but only those that have interface implemented will react to that message.

This is all correct, just the object that is sending the message is not checking if another object is implementing the message - it’s just calling that interface function and if it’s not implemented it will be ignored.

Just to add, if you need to, you can check if object is implementing an interface. This is Blueprint equivalent, but you can probably find C++ function in KismetSystemLibrary: Does Implement Interface | Unreal Engine Documentation

Thanks again.

That was my first attemp, if I inherit from the interface but I don’t declare the method compilers complain:

TimeHandler.h(22): [C2259] ‘ATimeHandler’: cannot instantiate abstract class

EDIT: Oh nvm, I don’t need to inherit the interface from the object that sends the message, that makes much more sense.