2026-11-28 15:48:35 +08:00
|
|
|
local Log = require("Framework.Log")
|
|
|
|
|
local EventBus = require("Framework.EventBus")
|
|
|
|
|
local HotReload = require("Framework.HotReload")
|
|
|
|
|
local DEMO_COLORS = {
|
2026-11-29 03:08:03 +08:00
|
|
|
{
|
|
|
|
|
Name = "Cyan",
|
|
|
|
|
R = 0.08,
|
|
|
|
|
G = 0.78,
|
|
|
|
|
B = 0.95,
|
|
|
|
|
A = 1
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name = "Gold",
|
|
|
|
|
R = 0.98,
|
|
|
|
|
G = 0.72,
|
|
|
|
|
B = 0.12,
|
|
|
|
|
A = 1
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name = "Magenta",
|
|
|
|
|
R = 0.92,
|
|
|
|
|
G = 0.22,
|
|
|
|
|
B = 0.65,
|
|
|
|
|
A = 1
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name = "Lime",
|
|
|
|
|
R = 0.25,
|
|
|
|
|
G = 0.9,
|
|
|
|
|
B = 0.35,
|
|
|
|
|
A = 1
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name = "Orange",
|
|
|
|
|
R = 0.98,
|
|
|
|
|
G = 0.42,
|
|
|
|
|
B = 0.14,
|
|
|
|
|
A = 1
|
|
|
|
|
}
|
2026-11-28 15:48:35 +08:00
|
|
|
}
|
2026-11-29 03:08:03 +08:00
|
|
|
---
|
|
|
|
|
-- @noSelf
|
2026-11-28 15:48:35 +08:00
|
|
|
local function MakeLinearColor(c)
|
|
|
|
|
return UE.FLinearColor(c.R, c.G, c.B, c.A)
|
|
|
|
|
end
|
2026-11-29 03:08:03 +08:00
|
|
|
local LuaDemoActor = UnLua.Class()
|
|
|
|
|
function LuaDemoActor.ReceiveBeginPlay(self)
|
|
|
|
|
self.ElapsedTime = 0
|
|
|
|
|
self.TextRefreshTimer = 0
|
2026-11-28 15:48:35 +08:00
|
|
|
self.TickCount = 0
|
|
|
|
|
self.PingCount = 0
|
|
|
|
|
self.ReloadCount = 0
|
2026-11-29 03:08:03 +08:00
|
|
|
self.ColorIndex = 0
|
2026-11-28 15:48:35 +08:00
|
|
|
self.HasLoggedTick = false
|
2026-11-29 03:08:03 +08:00
|
|
|
self._tmpDeltaRot = UE.FRotator(0, 0, 0)
|
2026-11-28 15:48:35 +08:00
|
|
|
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)
|
2026-11-29 03:08:03 +08:00
|
|
|
UE.UKismetSystemLibrary.PrintString(
|
|
|
|
|
self,
|
|
|
|
|
msg,
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
UE.FLinearColor(0.2, 1, 0.4, 1),
|
|
|
|
|
8
|
|
|
|
|
)
|
2026-11-28 15:48:35 +08:00
|
|
|
end
|
2026-11-29 03:08:03 +08:00
|
|
|
function LuaDemoActor.ReceiveTick(self, DeltaSeconds)
|
|
|
|
|
self.ElapsedTime = (self.ElapsedTime or 0) + DeltaSeconds
|
2026-11-28 15:48:35 +08:00
|
|
|
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
|
2026-11-29 03:08:03 +08:00
|
|
|
local yawDelta = (self.SpinSpeed or 60) * DeltaSeconds
|
2026-11-28 15:48:35 +08:00
|
|
|
local pitchDelta = yawDelta * 0.35
|
2026-11-29 03:08:03 +08:00
|
|
|
if not self._tmpDeltaRot then
|
|
|
|
|
self._tmpDeltaRot = UE.FRotator(pitchDelta, yawDelta, 0)
|
|
|
|
|
else
|
|
|
|
|
self._tmpDeltaRot.Pitch = pitchDelta
|
|
|
|
|
self._tmpDeltaRot.Yaw = yawDelta
|
|
|
|
|
self._tmpDeltaRot.Roll = 0
|
|
|
|
|
end
|
|
|
|
|
self:AddCubeLocalRotation(self._tmpDeltaRot)
|
|
|
|
|
local zOffset = math.sin(self.ElapsedTime * (self.BobFrequency or 2)) * (self.BobAmplitude or 25)
|
2026-11-28 15:48:35 +08:00
|
|
|
self:SetCubeVerticalOffset(zOffset)
|
2026-11-29 03:08:03 +08:00
|
|
|
self.TextRefreshTimer = (self.TextRefreshTimer or 0) + DeltaSeconds
|
2026-11-28 15:48:35 +08:00
|
|
|
if self.TextRefreshTimer >= 0.2 then
|
2026-11-29 03:08:03 +08:00
|
|
|
self.TextRefreshTimer = 0
|
2026-11-28 15:48:35 +08:00
|
|
|
self:RefreshStatusText()
|
|
|
|
|
end
|
|
|
|
|
end
|
2026-11-29 03:08:03 +08:00
|
|
|
function LuaDemoActor.OnDemoPing(self, sourceName)
|
2026-11-28 15:48:35 +08:00
|
|
|
self.PingCount = (self.PingCount or 0) + 1
|
2026-11-29 03:08:03 +08:00
|
|
|
self.ColorIndex = ((self.ColorIndex or 0) + 1) % #DEMO_COLORS
|
2026-11-28 15:48:35 +08:00
|
|
|
local colorEntry = self:ApplyCurrentColor()
|
|
|
|
|
self:RefreshStatusText()
|
2026-11-29 03:08:03 +08:00
|
|
|
local msg = string.format(
|
|
|
|
|
"[UnLua Demo] Ping #%d from %s -> Cube color: %s",
|
|
|
|
|
self.PingCount,
|
|
|
|
|
tostring(sourceName),
|
|
|
|
|
colorEntry.Name
|
|
|
|
|
)
|
2026-11-28 15:48:35 +08:00
|
|
|
Log.Info(msg)
|
2026-11-29 03:08:03 +08:00
|
|
|
UE.UKismetSystemLibrary.PrintString(
|
|
|
|
|
self,
|
|
|
|
|
msg,
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
MakeLinearColor(colorEntry),
|
|
|
|
|
3
|
|
|
|
|
)
|
2026-11-28 15:48:35 +08:00
|
|
|
end
|
2026-11-29 03:08:03 +08:00
|
|
|
function LuaDemoActor.OnAfterHotReload(self, moduleName)
|
2026-11-28 15:48:35 +08:00
|
|
|
self.ReloadCount = (self.ReloadCount or 0) + 1
|
|
|
|
|
self:RefreshStatusText()
|
2026-11-29 03:08:03 +08:00
|
|
|
local msg = string.format(
|
|
|
|
|
"[UnLua Demo] HotReload #%d completed (target=%s)",
|
|
|
|
|
self.ReloadCount,
|
|
|
|
|
tostring(moduleName)
|
|
|
|
|
)
|
2026-11-28 15:48:35 +08:00
|
|
|
Log.Info(msg)
|
2026-11-29 03:08:03 +08:00
|
|
|
UE.UKismetSystemLibrary.PrintString(
|
|
|
|
|
self,
|
|
|
|
|
msg,
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
UE.FLinearColor(1, 0.85, 0.2, 1),
|
|
|
|
|
4
|
|
|
|
|
)
|
2026-11-28 15:48:35 +08:00
|
|
|
end
|
2026-11-29 03:08:03 +08:00
|
|
|
function LuaDemoActor.ApplyCurrentColor(self)
|
|
|
|
|
local colorEntry = DEMO_COLORS[(self.ColorIndex or 0) + 1] or DEMO_COLORS[1]
|
2026-11-28 15:48:35 +08:00
|
|
|
self:SetCubeColor(MakeLinearColor(colorEntry))
|
|
|
|
|
return colorEntry
|
|
|
|
|
end
|
2026-11-29 03:08:03 +08:00
|
|
|
function LuaDemoActor.RefreshStatusText(self)
|
|
|
|
|
local colorEntry = DEMO_COLORS[(self.ColorIndex or 0) + 1] or DEMO_COLORS[1]
|
2026-11-28 15:48:35 +08:00
|
|
|
local text = string.format(
|
|
|
|
|
"UnLua Active (%s)\nTime: %.1fs | Pings: %d | Reloads: %d | Color: %s\n[Space] Ping Lua | [R] HotReload",
|
|
|
|
|
_VERSION,
|
2026-11-29 03:08:03 +08:00
|
|
|
self.ElapsedTime or 0,
|
2026-11-28 15:48:35 +08:00
|
|
|
self.PingCount or 0,
|
|
|
|
|
self.ReloadCount or 0,
|
|
|
|
|
colorEntry.Name
|
|
|
|
|
)
|
|
|
|
|
self:SetStatusText(text)
|
|
|
|
|
end
|
2026-11-29 03:08:03 +08:00
|
|
|
function LuaDemoActor.ReceiveEndPlay(self, EndPlayReason)
|
2026-11-28 15:48:35 +08:00
|
|
|
EventBus.OffAllForOwner(self)
|
|
|
|
|
Log.Info("LuaDemoActor:ReceiveEndPlay(ticks=%d, pings=%d)", self.TickCount or 0, self.PingCount or 0)
|
|
|
|
|
end
|
2026-11-29 03:08:03 +08:00
|
|
|
return LuaDemoActor
|