feat: add L_LuaDemo scene and verify UnLua Tick, input, and hot-reload

This commit is contained in:
szy73711 committed 2026-11-28 15:48:35 +08:00
1 parent 943af16954
commit 256a60ac80
13 files changed
+670 -18

No files matched your search

+48 -12
View File
@@ -8,8 +8,19 @@ local HotReload = {}
HotReload.EVENT_BEFORE_RELOAD = "Framework.BeforeHotReload"
HotReload.EVENT_AFTER_RELOAD = "Framework.AfterHotReload"
---Reloads a specific Lua module (clears package.loaded and re-requires it).
---@param moduleName string Dot-separated Lua module name, e.g. "Game.LuaGameMode"
local function CollectReloadableGameModules()
local modules = {}
for name, _ in pairs(package.loaded) do
if type(name) == "string" and name:match("^Game%.") then
modules[#modules + 1] = name
end
end
table.sort(modules)
return modules
end
---Reloads a specific Lua module using UnLua's hot-reload sandbox (falls back to package.loaded reset).
---@param moduleName string Dot-separated Lua module name, e.g. "Game.LuaDemoActor"
---@return boolean ok
---@return any resultOrErr
function HotReload.ReloadModule(moduleName)
@@ -20,25 +31,50 @@ function HotReload.ReloadModule(moduleName)
Log.Info("Hot-reloading Lua module: %s", moduleName)
EventBus.Emit(HotReload.EVENT_BEFORE_RELOAD, moduleName)
package.loaded[moduleName] = nil
local ok, result = pcall(require, moduleName)
if not ok then
Log.Error("Failed to hot-reload module '%s': %s", moduleName, tostring(result))
return false, result
local okHr, unluaHr = pcall(require, "UnLua.HotReload")
if okHr and unluaHr and type(unluaHr.reload) == "function" then
local ok, err = pcall(unluaHr.reload, { moduleName })
if not ok then
Log.Error("UnLua.HotReload.reload('%s') failed: %s", moduleName, tostring(err))
return false, err
end
else
package.loaded[moduleName] = nil
local ok, result = pcall(require, moduleName)
if not ok then
Log.Error("Failed to hot-reload module '%s': %s", moduleName, tostring(result))
return false, result
end
end
EventBus.Emit(HotReload.EVENT_AFTER_RELOAD, moduleName, result)
EventBus.Emit(HotReload.EVENT_AFTER_RELOAD, moduleName)
Log.Info("Successfully hot-reloaded Lua module: %s", moduleName)
return true, result
return true
end
---Triggers UnLua's built-in hot reload across all loaded modules.
---Triggers UnLua hot-reload across all loaded Game.* modules (supports Saved/HotUpdate/Script overrides).
---@return boolean ok
function HotReload.ReloadAll()
Log.Info("Triggering full UnLua HotReload...")
local gameModules = CollectReloadableGameModules()
Log.Info("Triggering UnLua HotReload (modules=%d)...", #gameModules)
EventBus.Emit(HotReload.EVENT_BEFORE_RELOAD, "*")
if UnLua and type(UnLua.HotReload) == "function" then
local okHr, unluaHr = pcall(require, "UnLua.HotReload")
if okHr and unluaHr and type(unluaHr.reload) == "function" then
if #gameModules > 0 then
local ok, err = pcall(unluaHr.reload, gameModules)
if not ok then
Log.Error("UnLua.HotReload.reload(gameModules) failed: %s", tostring(err))
return false
end
end
-- Also run timestamp-based check for any other modified modules
local ok2, err2 = pcall(unluaHr.reload)
if not ok2 then
Log.Error("UnLua.HotReload.reload() failed: %s", tostring(err2))
return false
end
elseif UnLua and type(UnLua.HotReload) == "function" then
local ok, err = pcall(UnLua.HotReload)
if not ok then
Log.Error("UnLua.HotReload() failed: %s", tostring(err))
+109
View File
@@ -0,0 +1,109 @@
---@type LuaDemoActor
local M = UnLua.Class()
local Log = require("Framework.Log")
local EventBus = require("Framework.EventBus")
local HotReload = require("Framework.HotReload")
local DEMO_COLORS = {
{ Name = "Cyan", R = 0.08, G = 0.78, B = 0.95, A = 1.0 },
{ Name = "Gold", R = 0.98, G = 0.72, B = 0.12, A = 1.0 },
{ Name = "Magenta", R = 0.92, G = 0.22, B = 0.65, A = 1.0 },
{ Name = "Lime", R = 0.25, G = 0.90, B = 0.35, A = 1.0 },
{ Name = "Orange", R = 0.98, G = 0.42, B = 0.14, A = 1.0 },
}
local function MakeLinearColor(c)
return UE.FLinearColor(c.R, c.G, c.B, c.A)
end
function M:ReceiveBeginPlay()
self.ElapsedTime = 0.0
self.TextRefreshTimer = 0.0
self.TickCount = 0
self.PingCount = 0
self.ReloadCount = 0
self.ColorIndex = 1
self.HasLoggedTick = false
EventBus.On("Demo.Ping", self.OnDemoPing, self)
EventBus.On(HotReload.EVENT_AFTER_RELOAD, self.OnAfterHotReload, self)
self:ApplyCurrentColor()
self:RefreshStatusText()
Log.Info("LuaDemoActor:ReceiveBeginPlay() bound successfully! Lua=%s", _VERSION)
local msg = string.format("[UnLua Demo] LuaDemoActor active (%s) - Press [Space] to Ping, [R] to HotReload", _VERSION)
UE.UKismetSystemLibrary.PrintString(self, msg, true, true, UE.FLinearColor(0.2, 1.0, 0.4, 1.0), 8.0)
end
function M:ReceiveTick(DeltaSeconds)
self.ElapsedTime = (self.ElapsedTime or 0.0) + DeltaSeconds
self.TickCount = (self.TickCount or 0) + 1
if not self.HasLoggedTick then
self.HasLoggedTick = true
Log.Info("LuaDemoActor:ReceiveTick() first tick verified (dt=%.4f)", DeltaSeconds)
end
-- Rotate cube mesh & apply vertical sine-wave bobbing entirely from Lua
local yawDelta = (self.SpinSpeed or 60.0) * DeltaSeconds
local pitchDelta = yawDelta * 0.35
self:AddCubeLocalRotation(UE.FRotator(pitchDelta, yawDelta, 0.0))
local zOffset = math.sin(self.ElapsedTime * (self.BobFrequency or 2.0)) * (self.BobAmplitude or 25.0)
self:SetCubeVerticalOffset(zOffset)
self.TextRefreshTimer = (self.TextRefreshTimer or 0.0) + DeltaSeconds
if self.TextRefreshTimer >= 0.2 then
self.TextRefreshTimer = 0.0
self:RefreshStatusText()
end
end
function M:OnDemoPing(sourceName)
self.PingCount = (self.PingCount or 0) + 1
self.ColorIndex = (self.ColorIndex % #DEMO_COLORS) + 1
local colorEntry = self:ApplyCurrentColor()
self:RefreshStatusText()
local msg = string.format("[UnLua Demo] Ping #%d from %s -> Cube color: %s", self.PingCount, tostring(sourceName), colorEntry.Name)
Log.Info(msg)
UE.UKismetSystemLibrary.PrintString(self, msg, true, true, MakeLinearColor(colorEntry), 3.0)
end
function M:OnAfterHotReload(moduleName)
self.ReloadCount = (self.ReloadCount or 0) + 1
self:RefreshStatusText()
local msg = string.format("[UnLua Demo] HotReload #%d completed (target=%s)", self.ReloadCount, tostring(moduleName))
Log.Info(msg)
UE.UKismetSystemLibrary.PrintString(self, msg, true, true, UE.FLinearColor(1.0, 0.85, 0.2, 1.0), 4.0)
end
function M:ApplyCurrentColor()
local colorEntry = DEMO_COLORS[self.ColorIndex or 1] or DEMO_COLORS[1]
self:SetCubeColor(MakeLinearColor(colorEntry))
return colorEntry
end
function M:RefreshStatusText()
local colorEntry = DEMO_COLORS[self.ColorIndex or 1] or DEMO_COLORS[1]
local text = string.format(
"UnLua Active (%s)\nTime: %.1fs | Pings: %d | Reloads: %d | Color: %s\n[Space] Ping Lua | [R] HotReload",
_VERSION,
self.ElapsedTime or 0.0,
self.PingCount or 0,
self.ReloadCount or 0,
colorEntry.Name
)
self:SetStatusText(text)
end
function M:ReceiveEndPlay(EndPlayReason)
EventBus.OffAllForOwner(self)
Log.Info("LuaDemoActor:ReceiveEndPlay(ticks=%d, pings=%d)", self.TickCount or 0, self.PingCount or 0)
end
return M
+9 -1
View File
@@ -4,7 +4,15 @@ local M = UnLua.Class()
local Log = require("Framework.Log")
function M:ReceiveBeginPlay()
Log.Info("LuaGameMode:ReceiveBeginPlay()")
Log.Info("LuaGameMode:ReceiveBeginPlay() - Demo scene started")
UE.UKismetSystemLibrary.PrintString(
self,
"[UnLua Demo] LuaGameMode initialized!",
true,
true,
UE.FLinearColor(0.3, 0.85, 1.0, 1.0),
5.0
)
end
function M:ReceiveEndPlay(EndPlayReason)
+13 -1
View File
@@ -2,9 +2,21 @@
local M = UnLua.Class()
local Log = require("Framework.Log")
local EventBus = require("Framework.EventBus")
local HotReload = require("Framework.HotReload")
function M:ReceiveBeginPlay()
Log.Info("LuaPlayerController:ReceiveBeginPlay()")
Log.Info("LuaPlayerController:ReceiveBeginPlay() - Ready for [Space]=Ping, [R]=HotReload")
end
function M:OnDemoPingPressed()
Log.Info("LuaPlayerController:OnDemoPingPressed() -> Emitting Demo.Ping")
EventBus.Emit("Demo.Ping", "LuaPlayerController")
end
function M:OnDemoHotReloadPressed()
Log.Info("LuaPlayerController:OnDemoHotReloadPressed() -> Triggering HotReload.ReloadAll()")
HotReload.ReloadAll()
end
function M:ReceiveEndPlay(EndPlayReason)