AddOnActorSpawnedHandler Usage

To register a function using AddOnActorSpawnedHandler, you first need to create the delegate. So something like:




        void MyClass::RegisterSpawnHandler(UWorld* InWorld)
        {
	        FOnActorSpawned::FDelegate ActorSpawnedDelegate = FOnActorSpawned::FDelegate::CreateUObject(this, &MyClass::OnActorSpawned);
		InWorld->AddOnActorSpawnedHandler(ActorSpawnedDelegate);
	}

	void MyClass::OnActorSpawned(AActor* InActor)
	{
		//do something 
	}


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.

2 Likes