Last active
September 15, 2024 08:01
-
-
Save rtm223/b5fde2f18442fbded354be59a5023278 to your computer and use it in GitHub Desktop.
UE5 component to provide editor ticking functionality to Blueprints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Components/RTMEditorTickComponent.h" | |
namespace RTMEditorTickComponent_Statics | |
{ | |
#if RTMCOMMON_ENABLE_DEBUG_DISPLAY | |
static TAutoConsoleVariable<bool> CVarTickInEditor( | |
TEXT("rtm.Editor.AllowEditorTickComponent"), | |
true, | |
TEXT("Enables Editor Tick Components globally")); | |
#endif | |
} | |
URTMEditorTickComponent::URTMEditorTickComponent() | |
{ | |
PrimaryComponentTick.bCanEverTick = true; | |
PrimaryComponentTick.bStartWithTickEnabled = true; | |
PrimaryComponentTick.TickGroup = TG_PrePhysics; | |
bTickInEditor = true; | |
bIsEditorOnly = true; | |
} | |
void URTMEditorTickComponent::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
SetComponentTickEnabled(false); | |
} | |
void URTMEditorTickComponent::TickComponent(float deltaTime, ELevelTick tickType, FActorComponentTickFunction* thisTickFunction) | |
{ | |
Super::TickComponent(deltaTime, tickType, thisTickFunction); | |
const bool canEditorTick = bAllowEditorTick | |
&& RTMEditorTickComponent_Statics::CVarTickInEditor.GetValueOnAnyThread() | |
&& GetWorld() | |
&& GetWorld()->WorldType != EWorldType::PIE; | |
if(canEditorTick) | |
{ | |
OnEditorTick.Broadcast(deltaTime); | |
FEditorScriptExecutionGuard ScriptGuard; | |
K2_OnEditorTick.Broadcast(deltaTime); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "CoreMinimal.h" | |
#include "Components/ActorComponent.h" | |
#include "RTMEditorTickComponent.generated.h" | |
/* Simple component that will tick in editor and provide the owning Actor BP with an On Editor Tick event */ | |
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent), HideCategories=("Sockets", "ComponentTick", "Activation", "Replication","AssetUserData","Navigation", "Tags")) | |
class RTMCOMMON_API URTMEditorTickComponent : public UActorComponent | |
{ | |
GENERATED_BODY() | |
public: | |
URTMEditorTickComponent(); | |
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnEditorTickDelegate, float, deltaSeconds); | |
DECLARE_EVENT_OneParam(ThisClass, FOnEditorTickEvent, float deltaSeconds); | |
FOnEditorTickEvent OnEditorTick; | |
protected: | |
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Editor") | |
bool bAllowEditorTick = true; | |
/* Produces a tick while in editor scenes | |
* Does NOT tick in PIE or packaged builds | |
* Can be suppressed globally using CVar 'rtm.Editor.AllowEditorTickComponent=false' | |
or per-instance using the 'Allow Editor Tick' checkbox on the component */ | |
UPROPERTY(BlueprintAssignable, Category="Editor", meta=(DisplayName="On Editor Tick")) | |
FOnEditorTickDelegate K2_OnEditorTick; | |
virtual void BeginPlay() override; | |
virtual void TickComponent(float deltaTime, ELevelTick tickType, FActorComponentTickFunction* thisTickFunction) override; | |
private: | |
FTimerHandle TimerHandle; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Editor Tick Component
Add this component to an Actor Blueprint and select its "On Editor Tick" event to receive events in editor preview windows
Works in both the BP viewport and in levels
Can be suppressed using CVar
rtm.Editor.AllowEditorTickComponent=false
(global) or unchecking theAllow Editor Tick
checkbox (per instance)