void AGameplayDebuggerReplicator::OnDebugAIDelegate(class UCanvas* Canvas, class APlayerController* PC)
{
#if WITH_EDITOR && ENABLED_GAMEPLAY_DEBUGGER
	if (!GIsEditor)
	{
		return;
	}

	if (!LocalPlayerOwner)
	{
		return;
	}

	UEditorEngine* EEngine = Cast<UEditorEngine>(GEngine);
	if (!EEngine || !EEngine->bIsSimulatingInEditor)
	{
		return;
	}

	if (!Canvas || !Canvas->SceneView || Canvas->SceneView->bIsGameView == false)
	{
		return;
	}

	FEngineShowFlags EngineShowFlags = Canvas && Canvas->SceneView && Canvas->SceneView->Family ? Canvas->SceneView->Family->EngineShowFlags : FEngineShowFlags(GIsEditor ? EShowFlagInitMode::ESFIM_Editor : EShowFlagInitMode::ESFIM_Game);
	if (!EngineShowFlags.DebugAI)
	{
		return;
	}

	EnableDraw(true);
	UWorld* World = GetWorld();
	if (World && Role == ROLE_Authority)
	{
		if (IsActorTickEnabled() == false)
		{
			SetActorTickEnabled(true);
		}

		// looks like Simulate in UE4 Editor - let's find selected Pawn to debug
		AActor* SelectedActor = nullptr;
		for (FSelectionIterator It = EEngine->GetSelectedActorIterator(); It; ++It)
		{
			SelectedActor = Cast<APawn>(*It); //we only work with pawns for now
			if (SelectedActor)
			{
				break;
			}
		}

		if (LastSelectedActorToDebug != SelectedActor)
		{
			MarkComponentsRenderStateDirty();
		}

		ServerSetActorToDebug(SelectedActor);
		DrawDebugData(Canvas, PC, true);
	}
#endif
}
void AGameplayDebuggingReplicator::BeginDestroy()
{
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
	if (GEngine)
	{
		GEngine->bEnableOnScreenDebugMessages = true;
	}
	if (IsDrawEnabled())
	{
		EnableDraw(false);
	}
#endif
	Super::BeginDestroy();
}
void AGameplayDebuggingReplicator::OnDebugAIDelegate(class UCanvas* Canvas, class APlayerController* PC)
{
#if WITH_EDITOR && !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
	if (!GIsEditor)
	{
		return;
	}

	if (!LocalPlayerOwner || IsGlobalInWorld())
	{
		return;
	}

	UEditorEngine* EEngine = Cast<UEditorEngine>(GEngine);
	if (GFrameNumber == LastDrawAtFrame || !EEngine || !EEngine->bIsSimulatingInEditor)
	{
		return;
	}

	if (!Canvas || !Canvas->SceneView || Canvas->SceneView->bIsGameView == false)
	{
		return;
	}
	LastDrawAtFrame = GFrameNumber;

	FEngineShowFlags EngineShowFlags = Canvas && Canvas->SceneView && Canvas->SceneView->Family ? Canvas->SceneView->Family->EngineShowFlags : FEngineShowFlags(GIsEditor ? EShowFlagInitMode::ESFIM_Editor : EShowFlagInitMode::ESFIM_Game);
	if (!EngineShowFlags.DebugAI)
	{
		return;
	}

	EnableDraw(true);
	UWorld* World = GetWorld();
	UGameplayDebuggingComponent* DebuggingComponent = GetDebugComponent();
	if (World && DebuggingComponent && DebuggingComponent->GetOwnerRole() == ROLE_Authority)
	{
		UGameplayDebuggingControllerComponent*  GDC = FindComponentByClass<UGameplayDebuggingControllerComponent>();
		TArray<int32> OryginalReplicateViewDataCounters;

		OryginalReplicateViewDataCounters = DebuggingComponent->ReplicateViewDataCounters;
		for (uint32 Index = 0; Index < EAIDebugDrawDataView::MAX; ++Index)
		{
			DebuggingComponent->ReplicateViewDataCounters[Index] = GameplayDebuggerSettings(this).CheckFlag((EAIDebugDrawDataView::Type)Index) ? 1 : 0;
		}

		// looks like Simulate in UE4 Editor - let's find selected Pawn to debug
		AActor* FullSelectedTarget = NULL;
		for (FConstPawnIterator Iterator = World->GetPawnIterator(); Iterator; ++Iterator)
		{
			AActor* NewTarget = Cast<AActor>(*Iterator);

			if (NewTarget->IsSelected() && !FullSelectedTarget)
			{
				FullSelectedTarget = NewTarget;
				continue;
			}

			//We needs to collect data manually in Simulate
			DebuggingComponent->SetActorToDebug(NewTarget);
			DebuggingComponent->CollectDataToReplicate(NewTarget->IsSelected());
			DrawDebugData(Canvas, PC);
		}

		const AActor* OldActor = LastSelectedActorToDebug;
		ServerSetActorToDebug(FullSelectedTarget);
		if (FullSelectedTarget)
		{
			DebuggingComponent->CollectDataToReplicate(true);
			DebuggingComponent->SetEQSIndex(ActiveEQSIndex);
			DrawDebugData(Canvas, PC);
		}

		if (GetSelectedActorToDebug() != OldActor)
		{
			DebuggingComponent->MarkRenderStateDirty();
		}

		DebuggingComponent->ReplicateViewDataCounters = OryginalReplicateViewDataCounters;

	}
#endif
}