Binding to FCoreUObjectDelegates::PostDemoPlay

Got it working: See EDIT2 at bottom!

Hi!

I’m trying to bind to the FCoreUObjectDelegates::PostDemoPlay delegate.
Currently, I have


FCoreUObjectDelegates::PostDemoPlay.AddUObject(this, &ATeamPlayerController::DemoStarted);

inside ATeamPlayerController
which is executed upon an EXEC command from the console command in-game or directly at the start of the match (in the gamemode cycle when it goes to spectator mode).
Unfortunately, it crashes at


FDelegateHandle Add( const FDelegate& Delegate )
	{
		FDelegateHandle             Result;
		TDelegateInstanceInterface* DelegateInstance = (TDelegateInstanceInterface*)Delegate.GetDelegateInstance();

		if (DelegateInstance != nullptr)
		{
			Result = AddDelegateInstance(DelegateInstance->CreateCopy());  // CRASH HERE !!!
		}

		return Result;
	}

At least


.AddUObject

compiles…

Other variants:


.AddDynamic

yields the compiler error: /TeamPlayerController.cpp:100:45: no member named ‘__Internal_AddDynamic’ in ‘TMulticastDelegate’


.AddRaw

yields: /Engine/Source/Runtime/Core/Public/Delegates/DelegateInstancesImpl_Variadics.inl:411:2: error: static_assert failed “You cannot use raw method delegates with UObjects.”


.AddSP

yields: /Engine/Source/Runtime/Core/Public/Delegates/DelegateSignatureImpl_Variadics.inl:155:64: error: no member named ‘AsShared’ in ‘ATeamPlayerController’

I suppose you can see that I’m rather unexperienced with delegates.
Am I binding at the wrong point? In the wrong class?
Probably it doesn’t matter which FSimpleMulticastDelegate from FCoreUObjectDelegates I take (tested


.AddUObject

with FCoreUObjectDelegates::PostLoadMap which results in the same crash).

So, obviously I’m doing something completely wrong. Just, what is it?

EDIT:
Tried .AddUObject in the exec command only. So I call:

  1. exec command to bind to PostDemoPlay
  2. demoplay test

The problem I guess is that demoplay will call UEngine::LoadMap which will destroy the player controllers and with them my bound functions…

Where would I then hook up the delegate binding? LocalPlayer?

EDIT2:
Okay, I successfully bound to PostDemoPlay by subclassing ULocalPlayer and using


void UMyLocalPlayer::PostInitProperties()
{
    Super::PostInitProperties();
    
    FCoreUObjectDelegates::PostDemoPlay.AddUObject(this, &UMyLocalPlayer::DemoStarted);
}

which works great. Problem now is, I’d like to spawn another PlayerController. But in the demo it uses PendingPlayerControllerClass… Ui, I guess this is another story : /