Skip to content

Instantly share code, notes, and snippets.

@rocifier
Created December 16, 2025 12:01
Show Gist options
  • Select an option

  • Save rocifier/0d27cac795802a7a55faebb1fbe355d6 to your computer and use it in GitHub Desktop.

Select an option

Save rocifier/0d27cac795802a7a55faebb1fbe355d6 to your computer and use it in GitHub Desktop.
// ===============================================
// InfluenceMapSubsystem.cpp
// ===============================================
#include "InfluenceMapSubsystem.h"
#include "InfluenceMapComponent.h"
#include "Engine/World.h"
bool UInfluenceMapSubsystem::ShouldCreateSubsystem(UObject* Outer) const
{
if (!UWorldSubsystem::ShouldCreateSubsystem(Outer))
{
return false;
}
// Only create for game worlds (PIE, standalone, dedicated server)
UWorld* World = Cast<UWorld>(Outer);
if (World)
{
return World->WorldType == EWorldType::Game ||
World->WorldType == EWorldType::PIE ||
World->WorldType == EWorldType::Editor; // Allow in editor for EQS debugging
}
return false;
}
void UInfluenceMapSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
UE_LOG(LogStealthAI, Log, TEXT("InfluenceMapSubsystem: Initializing"));
// Don't create immediately - let it be lazy loaded
// This prevents issues during world initialization
}
void UInfluenceMapSubsystem::Deinitialize()
{
UE_LOG(LogStealthAI, Log, TEXT("InfluenceMapSubsystem: Deinitializing"));
CleanupInfluenceMap();
Super::Deinitialize();
}
void UInfluenceMapSubsystem::CreateInfluenceMap()
{
UWorld* World = GetWorld();
if (!World)
{
UE_LOG(LogStealthAI, Warning, TEXT("InfluenceMapSubsystem: No valid world"));
return;
}
// Create component as a direct child of the subsystem (no actor needed)
InfluenceMapComponent = NewObject<UInfluenceMapComponent>(
this,
UInfluenceMapComponent::StaticClass(),
TEXT("InfluenceMap"),
RF_Transient // Mark as transient so it doesn't get saved
);
if (InfluenceMapComponent)
{
// Don't register the component - we'll manage it manually
// This prevents issues with component lifecycle
UE_LOG(LogStealthAI, Log, TEXT("InfluenceMapSubsystem: Created influence map component"));
}
else
{
UE_LOG(LogStealthAI, Error, TEXT("InfluenceMapSubsystem: Failed to create influence map component"));
}
}
void UInfluenceMapSubsystem::CleanupInfluenceMap()
{
if (InfluenceMapComponent)
{
// Mark for garbage collection
InfluenceMapComponent->ConditionalBeginDestroy();
InfluenceMapComponent = nullptr;
}
}
UInfluenceMapComponent* UInfluenceMapSubsystem::GetInfluenceMap()
{
// Lazy creation - only create when first requested
if (!InfluenceMapComponent)
{
CreateInfluenceMap();
}
return InfluenceMapComponent;
}
void UInfluenceMapSubsystem::RebuildInfluenceMap()
{
if (InfluenceMapComponent)
{
InfluenceMapComponent->InitializeGrid();
if (InfluenceMapComponent->bUseCollisionChecks)
{
InfluenceMapComponent->RebuildGridWithCollision();
}
UE_LOG(LogStealthAI, Log, TEXT("InfluenceMapSubsystem: Rebuilt influence map"));
}
else
{
UE_LOG(LogStealthAI, Warning, TEXT("InfluenceMapSubsystem: Cannot rebuild - no influence map exists"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment