Guest User

BuildingISMComponent.cpp

a guest
Jul 22nd, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.92 KB | None | 0 0
  1. #include "BuildingISMComponent.h"
  2. #include "InstancedStaticMeshDelegates.h"
  3. #include "Engine/Engine.h"
  4.  
  5. UBuildingISMComponent::UBuildingISMComponent(const FObjectInitializer& ObjectInitializer)
  6.     : Super(ObjectInitializer)
  7.     , bDelegatesBound(false)
  8. {
  9.     // Enable per-instance custom data if needed
  10.     NumCustomDataFloats = 0;
  11. }
  12.  
  13. void UBuildingISMComponent::BeginPlay()
  14. {
  15.     Super::BeginPlay();
  16.     BindToInternalDelegates();
  17. }
  18.  
  19. void UBuildingISMComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
  20. {
  21.     UnbindFromInternalDelegates();
  22.     Super::EndPlay(EndPlayReason);
  23. }
  24.  
  25. bool UBuildingISMComponent::IsValidInstanceIndex(int32 Index) const
  26. {
  27.     return Index >= 0 && Index < GetInstanceCount();
  28. }
  29.  
  30. void UBuildingISMComponent::BindToInternalDelegates()
  31. {
  32.     if (bDelegatesBound)
  33.     {
  34.         return;
  35.     }
  36.  
  37.     FInstancedStaticMeshDelegates::OnInstanceIndexUpdated.AddUObject(this, &UBuildingISMComponent::HandleInstanceIndexUpdated);
  38.     bDelegatesBound = true;
  39. }
  40.  
  41. void UBuildingISMComponent::UnbindFromInternalDelegates()
  42. {
  43.     if (!bDelegatesBound)
  44.     {
  45.         return;
  46.     }
  47.  
  48.     // Unbind from the delegates
  49.     FInstancedStaticMeshDelegates::OnInstanceIndexUpdated.RemoveAll(this);
  50.  
  51.     bDelegatesBound = false;
  52. }
  53.  
  54. void UBuildingISMComponent::HandleInstanceIndexUpdated(UInstancedStaticMeshComponent* InComponent, TArrayView<const FInstancedStaticMeshDelegates::FInstanceIndexUpdateData> InIndexUpdates)
  55. {
  56.     // Process each update and broadcast the appropriate events
  57.     for (const auto& UpdateData : InIndexUpdates)
  58.     {
  59.         switch (UpdateData.Type)
  60.         {
  61.         case FInstancedStaticMeshDelegates::EInstanceIndexUpdateType::Added:
  62.         {
  63.             FTransform InstanceTransform;
  64.             if (IsValidInstanceIndex(UpdateData.Index) && GetInstanceTransform(UpdateData.Index, InstanceTransform, true))
  65.             {
  66.                 CachedInstanceTransforms.Add(UpdateData.Index, InstanceTransform);
  67.                 OnInstanceAdded.Broadcast(UpdateData.Index, InstanceTransform);
  68.             }
  69.             break;
  70.         }
  71.  
  72.         case FInstancedStaticMeshDelegates::EInstanceIndexUpdateType::Removed:
  73.         {
  74.             // We can't get the transform of a removed instance, so use cached data if available
  75.             FTransform LastTransform = FTransform::Identity;
  76.             if (CachedInstanceTransforms.Contains(UpdateData.Index))
  77.             {
  78.                 LastTransform = CachedInstanceTransforms[UpdateData.Index];
  79.                 CachedInstanceTransforms.Remove(UpdateData.Index);
  80.             }
  81.  
  82.             OnInstanceRemoved.Broadcast(UpdateData.Index, LastTransform);
  83.             break;
  84.         }
  85.  
  86.         case FInstancedStaticMeshDelegates::EInstanceIndexUpdateType::Relocated:
  87.         {
  88.             // Update cached transforms
  89.             if (CachedInstanceTransforms.Contains(UpdateData.OldIndex))
  90.             {
  91.                 FTransform MovedTransform = CachedInstanceTransforms[UpdateData.OldIndex];
  92.                 CachedInstanceTransforms.Remove(UpdateData.OldIndex);
  93.  
  94.                 if (IsValidInstanceIndex(UpdateData.Index))
  95.                 {
  96.                     // Get the actual new transform
  97.                     FTransform ActualTransform = MovedTransform; // Initialize with fallback
  98.  
  99.                     if (!GetInstanceTransform(UpdateData.Index, ActualTransform, true))
  100.                     {
  101.                         // Log warning if needed - using cached transform as fallback
  102.                         UE_LOG(LogTemp, Warning, TEXT("Failed to get actual transform for relocated instance %d"), UpdateData.Index);
  103.                     }
  104.  
  105.                     CachedInstanceTransforms.Add(UpdateData.Index, ActualTransform);
  106.                     OnInstanceMoved.Broadcast(UpdateData.OldIndex, MovedTransform, UpdateData.Index, ActualTransform);
  107.                 }
  108.             }
  109.             break;
  110.         }
  111.         }
  112.     }
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment