How do I subscribe to a variable's events inside component's blueprint?

I have a component that needs a reference to some other component from a parent. I created a variable to hold the reference and set it inside parent’s ‘BeginPlay’ event, like so:
image
I’ve also tried to move this initialization to Actor’s ConstructionScript but to no avail.
On of the things I want to do with this reference is to listen to its events (declared as DECLARE_DYNAMIC_MULTICAST_DELEGATE):


Unfortunately, this does not work - events don’t get fired.
I have a feelings it has something to do with ‘LockOnTarget’ being a variable. I know for sure these events are firing properly, I’ve tried putting them inside Actor’s blueprint and they work just fine. I know I can just proxy these events from Actor to Component yet I wonder is there a way to achieve this some other way?

Are both components in the same actor?

yep!

There’s more than one way to handle it (circumstantial) but I like the Event Dispatcher approach as it leaves the components unaware of one another. And there are no additional hard references to handle - fewer things to worry about!


The actor has 2 unrelated components:

image

The idea is that ActorComp1 talks to ActorComp2.

  • ActorComp1 calls an Event Dispatcher:

  • ActorComp2 will listen:

image

  • we actually need to make it listen; the parent actor knows about the both of them so you can use it as glue:

The binding can be fully dynamic, too. You can even have one component search for another in the parent and bind.


Now, whenever the CustomEvent_0 is fired in the ActorComp1, the ActorComp2 will print Hello. You can send data through dispatchers. You can send the reference to the component, too.

2 Likes

Thanks for the suggestion, but I need to call some functions on this reference, as well as subscribe to its events. It’s possible to abstract away these calls too, but I think it’ll just complicate things, creating more indirections (at least for now).
My reasoning is that if I need a hard reference to call functions, I might as well use this reference to listen to events.
What bothers me is I see no reason why my setup doesn’t work. I assume it has something to do with the reference having no value until Component’s parent sets it. I’ve tried debugging engine code to get some deeper understanding but failed miserably. I think I narrowed it down to FMulticastDelegateProperty::InstanceSubobjects call, but not sure if it’s the right one.