I have a generic actor class called ATestUsable. This class holds a delegate named OnUse. I would like to be able to add this actor as a child to other actors. However, whenever I do, I lose access to the OnUse delegate and I’m not sure how I’m supposed to get it.
you could bind in c++ and then call a custom OnUseBP(BlueprintimplementableEvent)
Yeah I have binding in C++ working but I have to do it from the parent class, otherwise I don’t know of a way to tell the child what function to call when the event is fired. That means every parent that uses this component needs to write some C++ to connect the components.
If I try to do this from the child class and call a UFUNCTION from there, I run into the same problem where I can’t seem to be able to set that UFUNCTION from the parent.
On the child, I can see the function to override here:
But on the parent that doesn’t exist:
Which I suppose makes sense since this is looking to override the parent’s functions. But I’m not sure how I’d go about setting it in the child if it doesn’t show here.
or in your parent class call a function off the event which calls a blueprint event.
Hmm I’m not sure how this would work since I can’t get the parent to bind to the event in the first place. Maybe I’m misunderstanding something though
i mean more like this, forgive my psudocode, and then override ChildBroadcast in the children. of course you may not even need the delegate in this case unless you want external actors to bind
Yeah that’s exactly what I had when I took the screenshots above! But I don’t understand how I would get my parent to listen to the child’s UFUNCTION if I’m overriding it in the child?
So I guess here is an example of where I have the event hooked up (using a delegate) right. But instead of printing the string, I want my parent to know that an event happened. But my child doesn’t know what it’s parent is or what function it needs to call (this should work for a variety of usable items), it should be up to the parent to tell the child. So I’m not sure where to go from there.
And similiarily, If I do it the UFUNCTION way, I get stuck in the same place:
so you can use BlueprintNativeEvent and just not override it?
or if you want both can you AddCallToParentFunction? i havent tried that on c++ events although i suppose it works on BeginPlay so should work
void Parent::BeginPlay() {
TArray<ATestUsable*> usables;
TArray<AActor*> actors;
GetAllChildActors(actors);
usables.Reserve(actors.Num());
for (auto actor : actors) {
if (auto casted = Cast<ATestUsable>(actor)) {
usables.Add(casted);
}
}
for (auto usable : usables) {
usable->OnUse.AddDynamic(this, &Parent::EventHappened);
}
}
void Parent::EventHappened(AActor actor) {
// respond to event here
}
The parent finds all actors and then goes through those and finds all ATestUsables. Then it binds those to a handler.
This works but it’s not great because my parent has to go through all it’s children and I have to write a version of this code for every parent. So I’m looking for ways to make this less painful to use.
or if you want both can you AddCallToParentFunction
If I’m not mistaken, this seems to be similar to calling Super? Which that would require my parents to extend my children instead of adding the children as a child component, which doesn’t seem quite what I’m looking for
Unfortunate I still need to do the cast even though the child actor component in the blueprint seems to understand what type my child actor class is. But makes sense why it’s like that.