Is there any tutorial or sample for usage of AddOnActorSpawnedHandler? In short what I want is, just a delegate function in a general-purpose class just like BlablaManager that gets a notification when an actor spawned to current level. Sorry, but I’m not familiar with delegation things so much
Hello WCode, I have already seen your topic, that is really useful but I couldn’t fit these tutorials to AddOnActorSpawnedHandler, most of the events are using signatures for registering themselves into the list, according to observer pattern, in my opinion. I’d be appreciated if you can help me to fit that to my problem
Although you might want to make the ActorSpawnedDelegate a class variable, so you can also remove it later (via World->RemoveOnActorSpawnedHandler). Also the :FDelegate::CreateUObject will vary depending on what type of class your function is in. So this example is in a class that is derived from UObject.
Thank you Matt, you are lifesaver As far as i understand, MyClass is a general purpose class, is that right? I mean for my understanding, in only one class I’m gonna declare the registration and all kind of aactor based objects will come to MyClass::OnActorSpawned in the same world. Or is it inversely?
MyClass was just a example name. Meaning you change that to whatever the name of the class you are using this in. So if you were using this in a actor class you had created, say named “MyCustomActor”. Then the code would be something like:
void AMyCustomActor::RegisterSpawnHandler(UWorld* InWorld)
FOnActorSpawned::FDelegate ActorSpawnedDelegate = FOnActorSpawned::FDelegate::CreateUObject(this, &AMyCustomActor::OnActorSpawned);
void AMyCustomActor::OnActorSpawned(AActor* InActor)
//a new actor (InActor) has been spawned in the world.
Then once you have called RegisterSpawnHandler (maybe from beginplay), every time a new actor is pawned in the world that Actor is in then OnActorSpawned should be called with the param being the actor that was spawned. Also as I said before, you also want to save the FOnActorSpawned::FDelegate ActorSpawnedDelegate as a class variable, so that when your actor instance is deleted you can remove the delegate.