跳至正文
版本:bleeding-edge 🩸

额外功能

沙盒游戏模式的一些额外功能和概念

沙盒游戏模式还提供了其他几个子系统,可以通过脚本进行使用和扩展,例如:

Sandbox WebUI

你可以通过 Sandbox.HUD 变量直接访问沙盒 WebUI。

Tutorials

教程 UI 现在可以显示标题、简短描述以及按键。 它会被工具枪系统自动调用,用于显示当前装备工具的操作说明。

你也可以在自己的包中使用它来向玩家提供指引。 请查看下方公开的方法:

Client/Index.lua
-- Shows the Tutorials UI
---@param title string Tutorials title
---@param description string Description
---@param keys_data table Table of keys in the format
---@ { { key = "KeyName", description = "What it does" }, ... }
function Sandbox.Tutorials.Show(title, description, keys_data)

-- Hides the Tutorials UI
function Sandbox.Tutorials.Hide()

SpawnHistory

这是一个服务器端系统,用于追踪每个玩家生成的全部物品。 它通过将信息附加到生成的物品上来实现,因此你可以检查是谁(以及在何时)生成了特定物品。

你可以在服务器端调用这些函数,将物品添加到玩家的历史记录中,以便玩家可以通过按 X 键来撤销生成。

Server/Index.lua
-- Helper to add an item to the history of a player
function Sandbox.SpawnHistory.AddItemToHistory(player, item)

-- Destroys the last item in the history of a player, if any, and returns true, otherwise returns false
function Sandbox.SpawnHistory.DeleteItemFromHistory(player, index)

-- Helper to update an item ownership data
-- I.e. in cases an item gets modified and we need to update the new owner
function Sandbox.SpawnHistory.UpdateItemOwnership(player, item)
tip

通过调用 SpawnHistory.AddItemToHistory(),会在实体上设置一个值 "SpawnedBy",该值会同步给所有客户端,并包含以下数据:

{
player_name, -- Player name
player_steam_id, -- Player Steam ID
time, -- Time that it was spawned
}

Notifications

虽然游戏内置已经有了一个通知系统(Client.ShowNotification()),但沙盒提供了一个更高级的通知系统,允许你显示只展示一次的“教程类”通知:

Client/Index.lua
-- Adds the Notification on the Screen
---@param type NotificationType Type of the notification to display
---@param id string Unique ID used to store if the notification was already displayed to the player
---@param message string The message to display
---@param duration number Duration in seconds of the notification
---@param delay number Time in seconds to wait until display the notification
---@param force? boolean To force it to be displayed regardless if it was already displayed before
function Sandbox.Notifications.Add(type, id, message, duration, delay, force)

PersistentConfig

我们创建了一个系统,用于在使用工具枪(或任何其他自定义工具)时帮助持久化配置。 我们的工具枪使用它来在不同会话之间持久化在上下文菜单中修改的配置:

Client/Index.lua
-- Saves a config
function PersistentConfigSystem.SaveConfig(class_name, config, value)

-- Gets a config
function PersistentConfigSystem.GetConfig(class_name, config_name)

EntityInput

这是一个由我们的实体使用的系统,用于将自定义输入绑定到实体上的动作:

Client/Index.lua
-- Adds an input binding to an entity
function EntityInputSystem.AddEntityBinding(entity, name, binding, callback_pressed, callback_released)

-- Removes an input binding from an entity
function EntityInputSystem.RemoveEntityBinding(entity, name)

在内部,它通过为你想要的任何实体类简单定义 input_bindings 来自动工作。这会在选择该实体时向上下文菜单公开待设置的输入绑定:

Client/MyEntity.lua
-- 此实体的输入绑定
Thruster.input_bindings = {
{
label = "activate / deactivate",
callback_pressed = function(entity)
entity:CallRemoteEvent("SetActive", Reliability.Reliable, true)
end,
callback_released = function(entity)
entity:CallRemoteEvent("SetActive", Reliability.Reliable, false)
end,
},
{
label = "toggle",
callback_pressed = function(entity)
entity:CallRemoteEvent("SetActive", Reliability.Reliable, not entity:GetValue("Active"))
end,
},
}