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

武器手电筒

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

Terminal
# 安装 default-weapons 包
./NanosWorldServer.exe --cli install package default-weapons
Server/Index.lua
-- Loads the default-weapons (note: it's recommended to add it to your Package's packages_requirements instead)
Server.LoadPackage("default-weapons")

-- Spawns a Weapon from default-weapons package
my_weapon = AR4(Vector(), Rotator())

-- Spawns a Spot Light (parameters: location, rotation, color, light_type, intensity, attenuation_radius, cone_angle)
my_light = Light(Vector(), Rotator(), Color(0.73, 0.67, 0.42), LightType.Spot, 1000, 1000, 35)

-- Starts turned off, it's only turned on when someone picks up the weapon
my_light:SetVisibility(false)

-- Attaches the Light to the Weapon with offset X = 100 (at the weapon's front)
my_light:AttachTo(my_weapon)
my_light:SetRelativeLocation(Vector(100, 0, 0))

-- Stores the light on the Weapon
my_weapon:SetValue("Light", my_light)

-- Only when someone picks up this weapon, turns on the Flashlight
my_weapon:Subscribe("PickUp", function(weapon, character)
local light = weapon:GetValue("Light")
if (light ~= nil) then
light:SetVisibility(true)
end
end)

-- When the weapon is dropped, turns off the Flashlight
my_weapon:Subscribe("Drop", function(weapon, character)
local light = weapon:GetValue("Light")
if (light ~= nil) then
light:SetVisibility(false)
end
end)