From cb828d7834e906e7c347afca9db2f1c89611c6b1 Mon Sep 17 00:00:00 2001
From: David Gilbert <david.gilbert@rwth-aachen.de>
Date: Mon, 28 Sep 2020 10:33:16 +0200
Subject: [PATCH] - Added a (little hacky) way to load visualizations on editor
 startup without re-initializing textures. Keep an eye on that as it might
 break for various reasons.

---
 .../Private/GPUInstancedLineComponent.cpp     | 32 ++++++++++++++++---
 .../Public/GPUInstancedLineComponent.h        |  9 +++++-
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp b/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp
index c48e285..687d43f 100644
--- a/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp
+++ b/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp
@@ -18,10 +18,6 @@ UGPUInstancedLineComponent::UGPUInstancedLineComponent(const FObjectInitializer&
 	TextureWidth = 2048;
 	TextureHeight = 2048;
 
-	//PrimaryComponentTick.bCanEverTick = true;
-	//bTickInEditor = true;
-	//bAutoActivate = true;	
-	
 	SetWorldTransform(FTransform::Identity);
 	SetUsingAbsoluteLocation(true);
 	
@@ -43,6 +39,18 @@ UGPUInstancedLineComponent::UGPUInstancedLineComponent(const FObjectInitializer&
 	//DynamicLineMaterial = UMaterialInstanceDynamic::Create(LineMaterialInterface, GetTransientPackage());
 	SetMaterial(0, DynamicLineMaterial);
 	SetMobility(EComponentMobility::Static);
+
+#if WITH_EDITOR
+	bAutoActivate = true;
+	PrimaryComponentTick.bCanEverTick = true;
+	bTickInEditor = true;
+	SetComponentTickEnabled(true);
+#else
+	PrimaryComponentTick.bCanEverTick = false;
+	bTickInEditor = false;
+	SetComponentTickEnabled(false);
+#endif
+
 }
 
 UGPUInstancedLineComponent::~UGPUInstancedLineComponent()
@@ -407,6 +415,22 @@ void UGPUInstancedLineComponent::OnUpdateTransform(EUpdateTransformFlags UpdateT
 	SetWorldTransform(FTransform::Identity);
 }
 
+void UGPUInstancedLineComponent::TickComponent(float DeltaTime, ELevelTick TickType,
+	FActorComponentTickFunction* ThisTickFunction)
+{
+	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
+	if(TickCount < MaxTicksUntilInit)
+	{
+		TickCount++;
+	}
+	else
+	{
+		Init();
+		bTickInEditor = false;
+		SetComponentTickEnabled(false);
+	}
+}
+
 #endif
 
 
diff --git a/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h b/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h
index a7f01c4..2dbfe14 100644
--- a/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h
+++ b/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h
@@ -104,7 +104,9 @@ public:
 	virtual void PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent) override;
 	virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override; // need this for the widget as for some godforsaken reason the Chain event doesn't fire...
 	// This is ABSURDLY hacky
-	virtual void OnUpdateTransform(EUpdateTransformFlags UpdateTransformFlags, ETeleportType Teleport = ETeleportType::None) override;	
+	virtual void OnUpdateTransform(EUpdateTransformFlags UpdateTransformFlags, ETeleportType Teleport = ETeleportType::None) override;
+
+	virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
 #endif
 
 	//virtual TStructOnScope<FActorComponentInstanceData> GetComponentInstanceData() const override;
@@ -484,6 +486,11 @@ public:
 
 	UPROPERTY(Transient)
 	bool bIsInitialized = false;
+
+	private:
+	// hacky way to initialize components.
+	uint32 TickCount = 0;
+	uint32 MaxTicksUntilInit = 5;
 };
 
 
-- 
GitLab