feat: add L_LuaDemo scene and verify UnLua Tick, input, and hot-reload
This commit is contained in:
1 parent
943af16954
commit
256a60ac80
13 files changed
+670
-18
No files matched your search
@@ -1,8 +1,134 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "Core/LuaPlayerController.h"
|
||||
#include "Demo/LuaDemoActor.h"
|
||||
#include "UnrealLuaDemo.h"
|
||||
#include "Components/InputComponent.h"
|
||||
#include "Engine/World.h"
|
||||
#include "EngineUtils.h"
|
||||
#include "InputCoreTypes.h"
|
||||
#include "InputKeyEventArgs.h"
|
||||
#include "TimerManager.h"
|
||||
|
||||
void ALuaPlayerController::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
bShowMouseCursor = false;
|
||||
FInputModeGameOnly InputMode;
|
||||
SetInputMode(InputMode);
|
||||
}
|
||||
|
||||
void ALuaPlayerController::SetupInputComponent()
|
||||
{
|
||||
Super::SetupInputComponent();
|
||||
|
||||
if (InputComponent)
|
||||
{
|
||||
FInputKeyBinding& PingBinding = InputComponent->BindKey(EKeys::SpaceBar, IE_Pressed, this, &ALuaPlayerController::HandlePingKeyPressed);
|
||||
PingBinding.bConsumeInput = false;
|
||||
|
||||
FInputKeyBinding& ReloadBinding = InputComponent->BindKey(EKeys::R, IE_Pressed, this, &ALuaPlayerController::HandleHotReloadKeyPressed);
|
||||
ReloadBinding.bConsumeInput = false;
|
||||
}
|
||||
}
|
||||
|
||||
FString ALuaPlayerController::GetModuleName_Implementation() const
|
||||
{
|
||||
return LuaModuleName;
|
||||
}
|
||||
|
||||
void ALuaPlayerController::HandlePingKeyPressed()
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemo, Log, TEXT("ALuaPlayerController::HandlePingKeyPressed() -> dispatching OnDemoPingPressed to Lua"));
|
||||
OnDemoPingPressed();
|
||||
}
|
||||
|
||||
void ALuaPlayerController::HandleHotReloadKeyPressed()
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemo, Log, TEXT("ALuaPlayerController::HandleHotReloadKeyPressed() -> dispatching OnDemoHotReloadPressed to Lua"));
|
||||
OnDemoHotReloadPressed();
|
||||
}
|
||||
|
||||
void ALuaPlayerController::DemoVerifyInteraction()
|
||||
{
|
||||
UWorld* World = GetWorld();
|
||||
if (!World)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UE_LOG(LogUnrealLuaDemo, Log, TEXT("[DemoVerify] Scheduling simulated SpaceBar and R key presses after world ticks..."));
|
||||
|
||||
FTimerHandle Step1Handle;
|
||||
World->GetTimerManager().SetTimer(Step1Handle, [WeakSelf = TWeakObjectPtr<ALuaPlayerController>(this)]()
|
||||
{
|
||||
ALuaPlayerController* PC = WeakSelf.Get();
|
||||
if (!PC)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UE_LOG(LogUnrealLuaDemo, Log, TEXT("[DemoVerify] Step 1: Simulating SpaceBar Pressed/Released"));
|
||||
PC->InputKey(FInputKeyEventArgs::CreateSimulated(EKeys::SpaceBar, IE_Pressed, 1.0f));
|
||||
PC->InputKey(FInputKeyEventArgs::CreateSimulated(EKeys::SpaceBar, IE_Released, 0.0f));
|
||||
}, 0.35f, false);
|
||||
|
||||
FTimerHandle Step2Handle;
|
||||
World->GetTimerManager().SetTimer(Step2Handle, [WeakSelf = TWeakObjectPtr<ALuaPlayerController>(this)]()
|
||||
{
|
||||
ALuaPlayerController* PC = WeakSelf.Get();
|
||||
if (!PC)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UE_LOG(LogUnrealLuaDemo, Log, TEXT("[DemoVerify] Step 2: Simulating R Pressed/Released"));
|
||||
PC->InputKey(FInputKeyEventArgs::CreateSimulated(EKeys::R, IE_Pressed, 1.0f));
|
||||
PC->InputKey(FInputKeyEventArgs::CreateSimulated(EKeys::R, IE_Released, 0.0f));
|
||||
}, 0.70f, false);
|
||||
|
||||
FTimerHandle Step3Handle;
|
||||
World->GetTimerManager().SetTimer(Step3Handle, [WeakSelf = TWeakObjectPtr<ALuaPlayerController>(this)]()
|
||||
{
|
||||
ALuaPlayerController* PC = WeakSelf.Get();
|
||||
if (!PC || !PC->GetWorld())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ALuaDemoActor* DemoActor = nullptr;
|
||||
if (TActorIterator<ALuaDemoActor> It(PC->GetWorld()); It)
|
||||
{
|
||||
DemoActor = *It;
|
||||
}
|
||||
|
||||
if (!DemoActor)
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemo, Error, TEXT("[DemoVerify] FAILED: ALuaDemoActor not found in world."));
|
||||
FPlatformMisc::RequestExitWithStatus(false, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
const FString StatusStr = DemoActor->GetStatusTextString();
|
||||
const FRotator CubeRot = DemoActor->GetCubeLocalRotation();
|
||||
const bool bHasTickRotation = !CubeRot.IsNearlyZero();
|
||||
const bool bHasPing = StatusStr.Contains(TEXT("Pings: 1"));
|
||||
const bool bHasReload = StatusStr.Contains(TEXT("Reloads: 1"));
|
||||
|
||||
UE_LOG(LogUnrealLuaDemo, Log, TEXT("[DemoVerify] Step 3 Result -> CubeRot=%s | StatusText=\n%s"),
|
||||
*CubeRot.ToString(), *StatusStr);
|
||||
|
||||
if (bHasTickRotation && bHasPing && bHasReload)
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemo, Log, TEXT("[DemoVerify] PASSED: Tick rotation, SpaceBar Ping, and R HotReload all verified!"));
|
||||
PC->ConsoleCommand(TEXT("Quit"));
|
||||
}
|
||||
else
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemo, Error, TEXT("[DemoVerify] FAILED: TickRot=%d Ping=%d Reload=%d"),
|
||||
bHasTickRotation ? 1 : 0, bHasPing ? 1 : 0, bHasReload ? 1 : 0);
|
||||
FPlatformMisc::RequestExitWithStatus(false, 1);
|
||||
}
|
||||
}, 1.15f, false);
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
/**
|
||||
* Base PlayerController bound to Lua (Content/Script/Game/LuaPlayerController.lua).
|
||||
* Binds demo hotkeys ([Space] Ping Lua, [R] HotReload Lua) and forwards them to Lua via BlueprintImplementableEvent.
|
||||
*/
|
||||
UCLASS()
|
||||
class UNREALLUADEMO_API ALuaPlayerController : public APlayerController, public IUnLuaInterface
|
||||
@@ -16,9 +17,31 @@ class UNREALLUADEMO_API ALuaPlayerController : public APlayerController, public
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
virtual void BeginPlay() override;
|
||||
virtual void SetupInputComponent() override;
|
||||
virtual FString GetModuleName_Implementation() const override;
|
||||
|
||||
/**
|
||||
* Implemented in LuaPlayerController.lua to test C++ -> Lua -> EventBus flow.
|
||||
* Note: BlueprintImplementableEvent ensures UE always dispatches through ProcessEvent into UnLua
|
||||
* (unlike BlueprintNativeEvent on native C++ classes which UHT optimizes to bypass ProcessEvent).
|
||||
*/
|
||||
UFUNCTION(BlueprintImplementableEvent, Category = "LuaDemo")
|
||||
void OnDemoPingPressed();
|
||||
|
||||
/** Implemented in LuaPlayerController.lua to trigger live Lua hot-reload. */
|
||||
UFUNCTION(BlueprintImplementableEvent, Category = "LuaDemo")
|
||||
void OnDemoHotReloadPressed();
|
||||
|
||||
/** Automated self-test command (console: Demo.VerifyInteraction) that simulates Space & R and verifies Tick + Lua state. */
|
||||
UFUNCTION(Exec)
|
||||
void DemoVerifyInteraction();
|
||||
|
||||
protected:
|
||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Lua")
|
||||
FString LuaModuleName = TEXT("Game.LuaPlayerController");
|
||||
|
||||
private:
|
||||
void HandlePingKeyPressed();
|
||||
void HandleHotReloadKeyPressed();
|
||||
};
|
||||
@@ -0,0 +1,119 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "Demo/LuaDemoActor.h"
|
||||
#include "Components/StaticMeshComponent.h"
|
||||
#include "Components/TextRenderComponent.h"
|
||||
#include "Engine/StaticMesh.h"
|
||||
#include "Materials/MaterialInterface.h"
|
||||
#include "Materials/MaterialInstanceDynamic.h"
|
||||
#include "UObject/ConstructorHelpers.h"
|
||||
|
||||
ALuaDemoActor::ALuaDemoActor()
|
||||
{
|
||||
PrimaryActorTick.bCanEverTick = true;
|
||||
|
||||
SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
|
||||
SetRootComponent(SceneRoot);
|
||||
|
||||
CubeMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CubeMesh"));
|
||||
CubeMesh->SetupAttachment(SceneRoot);
|
||||
|
||||
static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeMeshAsset(TEXT("/Engine/BasicShapes/Cube.Cube"));
|
||||
if (CubeMeshAsset.Succeeded())
|
||||
{
|
||||
CubeMesh->SetStaticMesh(CubeMeshAsset.Object);
|
||||
}
|
||||
|
||||
static ConstructorHelpers::FObjectFinder<UMaterialInterface> BasicMatAsset(TEXT("/Engine/BasicShapes/BasicShapeMaterial.BasicShapeMaterial"));
|
||||
if (BasicMatAsset.Succeeded())
|
||||
{
|
||||
CubeMesh->SetMaterial(0, BasicMatAsset.Object);
|
||||
}
|
||||
|
||||
StatusText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("StatusText"));
|
||||
StatusText->SetupAttachment(SceneRoot);
|
||||
StatusText->SetRelativeLocation(FVector(0.0f, 0.0f, 120.0f));
|
||||
// Face -X toward PlayerStart placed at (-450, 0, 100)
|
||||
StatusText->SetRelativeRotation(FRotator(0.0f, 180.0f, 0.0f));
|
||||
StatusText->SetHorizontalAlignment(EHTA_Center);
|
||||
StatusText->SetVerticalAlignment(EVRTA_TextCenter);
|
||||
StatusText->SetWorldSize(26.0f);
|
||||
StatusText->SetTextRenderColor(FColor(120, 245, 255));
|
||||
StatusText->SetText(FText::FromString(TEXT("Waiting for UnLua...")));
|
||||
}
|
||||
|
||||
void ALuaDemoActor::BeginPlay()
|
||||
{
|
||||
InitialLocation = GetActorLocation();
|
||||
|
||||
if (CubeMesh)
|
||||
{
|
||||
CubeDynamicMaterial = CubeMesh->CreateAndSetMaterialInstanceDynamic(0);
|
||||
}
|
||||
|
||||
Super::BeginPlay();
|
||||
}
|
||||
|
||||
void ALuaDemoActor::Tick(float DeltaSeconds)
|
||||
{
|
||||
Super::Tick(DeltaSeconds);
|
||||
|
||||
// AActor::Tick skips ReceiveTick for pure native C++ classes (CLASS_Native without CLASS_CompiledFromBlueprint).
|
||||
// Explicitly call ReceiveTick so UnLua's Lua ReceiveTick override executes every frame.
|
||||
if (!GetClass()->HasAnyClassFlags(CLASS_CompiledFromBlueprint))
|
||||
{
|
||||
ReceiveTick(DeltaSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
FString ALuaDemoActor::GetModuleName_Implementation() const
|
||||
{
|
||||
return LuaModuleName;
|
||||
}
|
||||
|
||||
void ALuaDemoActor::SetStatusText(const FString& InText)
|
||||
{
|
||||
if (StatusText)
|
||||
{
|
||||
StatusText->SetText(FText::FromString(InText));
|
||||
}
|
||||
}
|
||||
|
||||
FString ALuaDemoActor::GetStatusTextString() const
|
||||
{
|
||||
return StatusText ? StatusText->Text.ToString() : FString();
|
||||
}
|
||||
|
||||
void ALuaDemoActor::SetCubeColor(FLinearColor NewColor)
|
||||
{
|
||||
if (!CubeDynamicMaterial && CubeMesh)
|
||||
{
|
||||
CubeDynamicMaterial = CubeMesh->CreateAndSetMaterialInstanceDynamic(0);
|
||||
}
|
||||
|
||||
if (CubeDynamicMaterial)
|
||||
{
|
||||
CubeDynamicMaterial->SetVectorParameterValue(TEXT("Color"), NewColor);
|
||||
}
|
||||
}
|
||||
|
||||
void ALuaDemoActor::AddCubeLocalRotation(FRotator DeltaRotation)
|
||||
{
|
||||
if (CubeMesh)
|
||||
{
|
||||
CubeMesh->AddLocalRotation(DeltaRotation);
|
||||
}
|
||||
}
|
||||
|
||||
FRotator ALuaDemoActor::GetCubeLocalRotation() const
|
||||
{
|
||||
return CubeMesh ? CubeMesh->GetRelativeRotation() : FRotator::ZeroRotator;
|
||||
}
|
||||
|
||||
void ALuaDemoActor::SetCubeVerticalOffset(float ZOffset)
|
||||
{
|
||||
if (CubeMesh)
|
||||
{
|
||||
CubeMesh->SetRelativeLocation(FVector(0.0f, 0.0f, ZOffset));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/Actor.h"
|
||||
#include "UnLuaInterface.h"
|
||||
#include "LuaDemoActor.generated.h"
|
||||
|
||||
class UStaticMeshComponent;
|
||||
class UTextRenderComponent;
|
||||
class UMaterialInstanceDynamic;
|
||||
|
||||
/**
|
||||
* Interactive demo actor driven by Lua (Content/Script/Game/LuaDemoActor.lua)
|
||||
* to visually and programmatically verify UnLua binding, Tick, EventBus, and HotReload.
|
||||
*/
|
||||
UCLASS(Blueprintable)
|
||||
class UNREALLUADEMO_API ALuaDemoActor : public AActor, public IUnLuaInterface
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
ALuaDemoActor();
|
||||
|
||||
virtual void BeginPlay() override;
|
||||
virtual void Tick(float DeltaSeconds) override;
|
||||
virtual FString GetModuleName_Implementation() const override;
|
||||
|
||||
/** Updates the floating 3D status text rendered above the cube. */
|
||||
UFUNCTION(BlueprintCallable, Category = "LuaDemo")
|
||||
void SetStatusText(const FString& InText);
|
||||
|
||||
/** Returns the current floating 3D status text string. */
|
||||
UFUNCTION(BlueprintPure, Category = "LuaDemo")
|
||||
FString GetStatusTextString() const;
|
||||
|
||||
/** Updates the dynamic material color of the demo cube. */
|
||||
UFUNCTION(BlueprintCallable, Category = "LuaDemo")
|
||||
void SetCubeColor(FLinearColor NewColor);
|
||||
|
||||
/** Returns the world location captured at BeginPlay (used for smooth bobbing). */
|
||||
UFUNCTION(BlueprintPure, Category = "LuaDemo")
|
||||
FVector GetInitialLocation() const { return InitialLocation; }
|
||||
|
||||
/** Rotates only the cube mesh while keeping the floating 3D label readable. */
|
||||
UFUNCTION(BlueprintCallable, Category = "LuaDemo")
|
||||
void AddCubeLocalRotation(FRotator DeltaRotation);
|
||||
|
||||
/** Returns the current relative rotation of the cube mesh. */
|
||||
UFUNCTION(BlueprintPure, Category = "LuaDemo")
|
||||
FRotator GetCubeLocalRotation() const;
|
||||
|
||||
/** Sets the vertical bob offset of the cube mesh relative to the actor root. */
|
||||
UFUNCTION(BlueprintCallable, Category = "LuaDemo")
|
||||
void SetCubeVerticalOffset(float ZOffset);
|
||||
|
||||
public:
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "LuaDemo")
|
||||
TObjectPtr<USceneComponent> SceneRoot;
|
||||
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "LuaDemo")
|
||||
TObjectPtr<UStaticMeshComponent> CubeMesh;
|
||||
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "LuaDemo")
|
||||
TObjectPtr<UTextRenderComponent> StatusText;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LuaDemo")
|
||||
float SpinSpeed = 60.0f;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LuaDemo")
|
||||
float BobAmplitude = 25.0f;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LuaDemo")
|
||||
float BobFrequency = 2.0f;
|
||||
|
||||
protected:
|
||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Lua")
|
||||
FString LuaModuleName = TEXT("Game.LuaDemoActor");
|
||||
|
||||
private:
|
||||
UPROPERTY(Transient)
|
||||
TObjectPtr<UMaterialInstanceDynamic> CubeDynamicMaterial;
|
||||
|
||||
FVector InitialLocation = FVector::ZeroVector;
|
||||
};
|
||||
Reference in new issue
Block a user