UE器材开发-问题反馈
<table>
<thead>
<tr>
<th>作者</th>
<th>QFord</th>
</tr>
</thead>
<tbody>
<tr>
<td>更新日期</td>
<td>2024-7-23</td>
</tr>
</tbody>
</table>
<p>1:器材开发规范(基于和永弟讨论2024-7-22)
<strong>总结:</strong>
1:器材主要负责配置和胶水代码功能
2:期望器材lua代码正常不超过100行(自动生成的除外)
3:器材中的组件的事件代码由用品编辑器自动生成,如电学引擎提供的数据变化事件
4:支持unlua和C++属性绑定(类似Loxodon Framework)</p>
<p>以<strong>螺线管</strong>为例:</p>
<pre><code class="language-lua">-- package.cpath = package.cpath .. &quot;;D:/VLabGit/vlab/virtual-lab-unreal/UnLua Debug/socket/?.dll&quot;
-- require(&quot;LuaPanda&quot;).start(&quot;127.0.0.1&quot;,8818)
---@type BP_LuoXianGuanElement_C//此代码可自动生成
local M = UnLua.Class()//此代码可自动生成
function M:ReceiveBeginPlay()//此函数代码可自动生成
local World = self:GetWorld()
-- 获取 BP_Engine_Magneit 的类引用 //条形磁铁组件可以自动生成代码10-17
local BP_Engine_Magneit_Class = UE.UClass.Load(&quot;/Game/VLabUE/Engine/Magneit/BP_Engine_Magneit.BP_Engine_Magneit_C&quot;)
local EngineMagneitInstance = UE.UGameplayStatics.GetActorOfClass(World, BP_Engine_Magneit_Class)
self.magnet = EngineMagneitInstance
self.BP_MagnetComponentBar:Init()
self:SetTempTransform()
self.BP_CircuitComponentResistor:Init()//电学引擎组件可自动生成代码18-19
self.BP_CircuitComponentResistor:SetResistance(3)//走配置
local BPFL_Electricity = UE.UClass.Load(&quot;/Game/VLabUE/Blueprints/CommonLibrary/BPFL_Electricity.BPFL_Electricity_C&quot;)//接线柱配置可自动生成代码21-23
BPFL_Electricity.BindingPort(self.JieXianZhu01, self.BP_CircuitComponentResistor, 0, World)
BPFL_Electricity.BindingPort(self.JieXianZhu02, self.BP_CircuitComponentResistor, 1, World)
self.BP_CircuitComponentResistor.DataChangedEventDispatcher:Add(self, self.OnResisitorDataChanged)//电学引擎组件事件可自动生成
self.IsInited = true//此代码可自动生成 27-29
self.IsShowMagneticLine = false
self.Mps = 0
end
function M:ReceiveTick(DeltaSeconds)//此函数代码可自动生成
if self.NeedUpdate then
self.NeedUpdate = false
self:UpdateMagnetData()
else
if self:IsTransformChanged() then
self:SetTempTransform()
self:UpdateMagnetData()
end
end
end
function M:I_ShowMagneticLine(bShow)//胶水代码-显示磁场绘线组件
self:ShowMagneticLine(bShow)
end
function M:SetTempTransform()//胶水代码-设置位置和旋转
local pos = self:K2_GetActorLocation()
self.TempLocation = pos
local rotator = self:K2_GetActorRotation()
self.TempRotation = rotator
end
function M:IsTransformChanged()//胶水代码-判断位置和旋转是否发生变化
local pos = self:K2_GetActorLocation()
local rotator = self:K2_GetActorRotation()
if pos ~= self.TempLocation or rotator ~= self.TempRotation then
return true
end
return false
end
function M:UpdateMagnetData()//胶水代码-更新磁铁数据
if self.IsInited then
self:SetMagnetPole()
self:ShowMagneticLine(self.IsShowMagneticLine)
end
end
function M:SetMagnetPole()//胶水代码-设置磁铁极性
local posN = self.NPole:K2_GetComponentLocation()
posN = UE.UUTransformUtils.ConvertUnrealToUnity(posN)
local posS = self.SPole:K2_GetComponentLocation()
posS = UE.UUTransformUtils.ConvertUnrealToUnity(posS)
local jsonData = &quot;{&quot;..&quot;'Nx':&quot;..posN.X..&quot;,'Ny':&quot;..posN.Y..&quot;,'Nz':&quot;..posN.Z..&quot;,'Sx':&quot;..posS.X ..&quot;,'Sy':&quot;..posS.Y..&quot;,'Sz':&quot;..posS.Z..&quot;,'MPS':&quot;..self.Mps..&quot;}&quot;
print(jsonData)
self.magnet:UpdateComponent(self.BP_MagnetComponentBar.CompId,jsonData)
end
function M:ShowMagneticLine(isShow)//胶水代码-控制磁场绘线组件的显示与隐藏
self.IsShowMagneticLine = isShow
if (self.IsInited) then
if (self.Mps &gt; 0 and self.IsShowMagneticLine) then
self:DrawMagneticLine()
else
self.BP_MagneticLineComponent:DestroyLines()
end
end
end
function M:GetStartPoints()//胶水代码-绘制起始点位置来自于器材中添加的绘制点
local childNodes = UE.TArray(UE.USceneComponent)
self.DrawPoints:GetChildrenComponents(false, childNodes)
local points = {}
for i = 1, childNodes:Length() do
local node = childNodes:Get(i)
local pos = node:K2_GetComponentLocation()
pos = UE.UUTransformUtils.ConvertUnrealToUnity(pos)
table.insert(points, pos)
end
return points
end
function M:DrawMagneticLine()//调用磁场绘线组件(磁场绘线组件是基于绘线组件扩展的版本)
self.BP_MagneticLineComponent:ResetLines()
local points = self:GetStartPoints()
if (#points &gt; 0) then
local linePath = UE.TArray(UE.FArrayOfVectors)
self.magnet:GetMagneticFieldLine(points, 36, 0.01, false, linePath)
for i = 1, linePath:Length() do
local drawPoints = linePath:Get(i).Vectors
local drawPoints = UE.UUTransformUtils.ConvertUnityToUnrealArray(drawPoints)
self.BP_MagneticLineComponent:DrawLine(drawPoints)
end
end
end
function M:OnResisitorDataChanged(compId, changedData)//胶水代码-绑定电学引擎组件事件
for i, v in pairs(changedData) do
if v.ParamKey == &quot;Current&quot; then
if (v.OldValue ~= v.NewValue) then
local newCurrent = tonumber(v.NewValue)
if newCurrent &gt; 0 then
self.Mps = 0.012245
self.NeedUpdate = true
end
end
return
end
end
end
return M
</code></pre>