跳至正文
版本:最新版 - a1.169.2 ⚖️

武器手电筒

这段示例代码将光源附加到武器上,来制作一个手电筒配件。

Terminal
# 安装 default-weapons 包
./NanosWorldServer.exe --cli install package default-weapons
Server/Index.lua
-- 加载 default-weapons(注:建议将其添加到包的 packages_requirements 中)
Server.LoadPackage("default-weapons")

-- 从 default-weapons 包生成一把武器
my_weapon = AR4(Vector(), Rotator())

-- 生成一个聚光灯(参数:位置、旋转、颜色、光源类型、强度、衰减半径、圆锥角)
my_light = Light(Vector(), Rotator(), Color(0.73, 0.67, 0.42), LightType.Spot, 1000, 1000, 35)

-- 初始为关闭状态,只有当有人拾取武器 时才会开启
my_light:SetVisibility(false)

-- 将光源附加到武器上,偏移量 X = 100(位于武器前方)
my_light:AttachTo(my_weapon)
my_light:SetRelativeLocation(Vector(100, 0, 0))

-- 将光源存储在武器上
my_weapon:SetValue("Light", my_light)

-- 仅当有人拾取此武器时,开启手电筒
my_weapon:Subscribe("PickUp", function(weapon, character)
local light = weapon:GetValue("Light")
if (light ~= nil) then
light:SetVisibility(true)
end
end)

-- 当武器被丢弃时,关闭手电筒
my_weapon:Subscribe("Drop", function(weapon, character)
local light = weapon:GetValue("Light")
if (light ~= nil) then
light:SetVisibility(false)
end
end)