Is there a way for an actor component to act on its owners event outside of casting the owner and binding to the event? For example I’m doing the below.
This is an actor component for an ability. Can I do this using an interface? For example have the interface have an OnPlayerDeath function then have the event in my ability? This seams cumbersome having to cast the owner in every ability then bind to those events in every ability. Feels like I’m doing this wrong.
Yes, you can, but maybe it’s easier to use event dispatchers and call a specific function inside each component every time a specific event is called from the owning actor.
That’s exactly what the above already is. I was hoping for a solution that wouldn’t require me to cast the owner to the owning class then bind to those dispatchers. I want to bypass needing to cast at all.
I’d like to just be able to directly add the dispatched event to my actor component, but my actor component has no idea what it’s attached to so it can’t access any of the owners information without casting or using an interface.
I think the only way I can do that is make an interface function that accepts an Event as an input then have the function on the player bind that event. This should prevent casting I think. Edit: Doesn’t work as I can’t set an input variable for a interface function to a Delegate or at least I can’t find it when trying to add such an input.
You can do it with an interface, you just need to add a parameter of the desired type to the interface (character in the example), so that you don’t need to cast it every time:
Yeah that’s basically what I’ve started implementing already. Was just hoping for a more elegant way using delegates, but is what it is. Wish interface supported pure functions too.
Personally, I prefer the event dispatcher you started with, I know it’s a best practice to avoid casts but I think that using one when the component is initialized is really not that bad.
Another approach, if you want to avoid the casting is creating a variable inside each component to reference the owner (and make it “Exposed on spawn”), when you create the component from the parent you pass “self” into it.
Main issue with setting up all these delegates is I’m ending up with hard references to the owner in each and every ability component. So far seams faster to just grab all components of X class and let the player object deal with notifying them via looping and calling their event.
I created a Component that plays a collision sound on any actor it is attached to. For example, differently shaped blocks that are falling to the ground.
In the Component Blueprint, I used the “Bind Event To On Actor Hit” node and pass that to a Custom Event Node.
“Bind Event To On Actor Hit” is actually using a delegate, the only difference is that is implemented by default in every Actor, this might not be applicable to every situation