Are delegates the answer?
Can I have an implementation example?
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.
(post deleted by author)
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.