AddOnActorSpawnedHandler Usage

Hello people,

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 :slight_smile:

And all stuff should be in c++, not in blueprint.

Thanks.

Burak

I’m currently reading about delegates as well, take a look at:

As for the actor spawning I’m pretty sure its basically calling the delegate on the Pawn class virtual void PostInitializeComponents() override;

Will you be passing arguments to the function?

Yes , I’m gonna pass the aactor* variable that is about to spawn.

Let me know if you achieve it, I’ll have to do something similar as well =)

Plus the documentation is outdated.

Also on the first post I said you should call the delegate, but what I meant was set the timer on the delegate.

I couldn’t get what you meant by setting the timer on the delegate? Actually I didn’t understand the relation between timing and direct callback.

Delegates on its own aren’t timers, just references to functions with some more capabilities;

You need to set timers on delegates,e.g.:
GetWorldTimerManager().SetTimer( YourDelegate, 5.0f, TRUE );

Hello i created a short tutorial on delegates hope it will be helpfull.

Hello , 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 :slight_smile:

As far as I know from documentation, signature should be FOnActorSpawned with one parameter, because

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UWorld/AddOnActorSpawnedHandler/index.html

AddOnActorSpawnedHandler is using FOnActorSpawned as parameter. But when I use FOnActorSpawned as a signature to use with DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam, that gives error.

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

Thank you Matt, you are lifesaver :slight_smile: 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);
	InWorld->AddOnActorSpawnedHandler(ActorSpawnedDelegate);
}

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.

2 Likes

That is the exact answer that was looking for, thank you so much for your help, I’m appreciated.

Burak

I’m 8 years later and you solved this problem for me. That’s so much!

Thanks a lot, it is 2023 and this again saved precious time!