Skip to content

Instantly share code, notes, and snippets.

@rtm223
Last active September 15, 2024 08:01
Show Gist options
  • Save rtm223/b5fde2f18442fbded354be59a5023278 to your computer and use it in GitHub Desktop.
Save rtm223/b5fde2f18442fbded354be59a5023278 to your computer and use it in GitHub Desktop.
UE5 component to provide editor ticking functionality to Blueprints
#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);
}
}
#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;
};
@rtm223
Copy link
Author

rtm223 commented Sep 15, 2024

Editor Tick Component

Add this component to an Actor Blueprint and select its "On Editor Tick" event to receive events in editor preview windows

image

Works in both the BP viewport and in levels
UnrealEditor-Win64-DebugGame_kgPvOi5HcJ
EditorDuck2

Can be suppressed using CVar rtm.Editor.AllowEditorTickComponent=false (global) or unchecking the Allow Editor Tick checkbox (per instance)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment