💻 服务器
Server 代表服务器端的所有服务器控件。

🎒 示例
Server/Index.lua
-- 服务器启动时打印“服务器已启动”
Server.Subscribe("Start", function()
Console.Log("服务器已启动")
end)
-- 当服务器停止/关机时,打印“服务器已停止”
Server.Subscribe("Stop", function()
Console.Log("服务器已停止")
end)
-- 每隔约 33 毫秒打印一次差值时间
Server.Subscribe("Tick", function(delta_time)
Console.Log("刻:" .. delta_time)
end)
🗿 静态函数
| Returns | Name | Description | |
|---|---|---|---|
AddMapSpawnPoint | 向当前地图的生成点列表中添加一个生成点 | ||
BanByAccountID | 通过玩家的账户 ID 将其从服务器封禁,非常适合在 PlayerConnect 事件内部调用 | ||
ChangeMap | 在新地图中重启服务器,重启所有包并重新连接所有玩家 | ||
| table of Base Actor | GetActorsInRadius | 返回指定半径内的 Actor | |
| table of string | GetAllValuesKeys | 获取所有值键的列表 | |
| integer | GetCompressionLevel | 返回当前使用的服务器压缩级别 | |
| integer | GetConnectionCount | 返回已连接的玩家数量(包括正在连接过程中的玩家) | |
| table of table | GetCustomSettings | 返回作为参数传入或通过新游戏屏幕设置的服务器自定义设置 | |
| integer | GetDefaultEntityDimension | 返回默认的实体维度 | |
| integer | GetDefaultPlayerDimension | 返回默认的玩家维度 | |
| string | GetDescription | 返回服务器描述 | |
| Base Entity or nil | GetEntityByID | 返回指定 ID 的实体 | |
| string | GetIP | 返回服务器的公网 IP | |
| integer | GetLogLevel | 返回当前的日志级别 | |
| string | GetMap | 返回当前地图的包名称 | |
| string | GetMapAsset | 返回当前地图资产的引用路径 | |
| table | GetMapConfig | 返回当前地图的自定义配置列表(存储在 MAP_NAME.toml 中) | |
| table of table | GetMapSpawnPoints | 返回当前地图的生成点列表(存储在 MAP_NAME.toml 中) | |
| integer | GetMaxPlayers | 返回配置的最大玩家数 | |
| string | GetName | 返回服务器名称 | |
| table of table | GetPackages | 返回正在运行的包列表,也可以选择返回服务器中安装的所有包 | |
| table of Player | GetPlayersInRadius | 返回指定半径内的玩家 | |
| integer | GetPort | 返回服务器端口 | |
| integer | GetQueryPort | 返回服务器 QueryPort | |
| integer | GetTickRate | 返回配置的刻速率 | |
| integer | GetTime | 获取毫秒级 Unix 时间戳 | |
| any | GetValue | 获取指定键的值 | |
| string | GetVersion | 返回服务器版本 | |
| boolean | IsAnnounced | 返回服务器是否已发布到主列表中 | |
| boolean | IsPackageLoaded | 返回指定的包当前是否已加载 | |
KickByAccountID | 通过玩家的账户 ID 将其从服务器踢出,非常适合在 PlayerConnect 事件内部调用 | ||
| boolean | LoadPackage | 加载一个包 | |
| boolean | ReloadPackage | No description provided | |
Restart | 重启服务器,重新加载所有包并重新连接所有玩家 | ||
SetDefaultEntityDimension | 设置实体生成时的默认服务器维度 | ||
SetDefaultPlayerDimension | 设置玩家生成时的默认服务器维度 | ||
SetDescription | 设置服务器的描述,可选择是否覆盖 Config.toml | ||
SetLogo | 设置服务器的徽标,可选择是否覆盖 Config.toml | ||
SetMaxPlayers | 设置服务器的最大玩家槽位数,可选择是否覆盖 Config.toml | ||
SetName | 设置服务器的名称,可选择是否覆盖 Config.toml | ||
SetPassword | 设置服务器的密码,可选择是否覆盖 Config.toml | ||
SetValue | 在服务器端设置一个全局值,可以从任何地方访问(服务器端) | ||
Stop | 停止服务器 | ||
| function | Subscribe | 订阅事件 | |
Unban | 通过账户 ID 解封玩家 | ||
| boolean | UnloadPackage | 卸载一个包 | |
Unsubscribe | 退订此特定类和此特定包中所有已订阅的事件,可选择传递函数以仅退订该回调 |
Optimal

AddMapSpawnPoint
向当前地图的生成点列表中添加一个生成点
Server.AddMapSpawnPoint(location, rotation?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| Vector | location | Required parameter | 生成位置 |
| Rotator | rotation? | Rotator() | 生成旋转 |
See also GetMapSpawnPoints.
Moderate

BanByAccountID
通过玩家的账户 ID 将其从服务器封禁,非常适合在 PlayerConnect 事件内部调用
Server.BanByAccountID(player_account_id, reason)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | player_account_id | Required parameter | No description provided |
| string | reason | Required parameter | No description provided |
Slow

ChangeMap
在新地图中重启服务器,重启所有包并重新连接所有玩家
Server.ChangeMap(map_path)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | map_path | Required parameter | 要加载的新地图路径 |
Moderate

GetActorsInRadius
返回指定半径内的 Actor
local ret = Server.GetActorsInRadius(location, radius, only_classes?, dimension?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| Vector | location | Required parameter | 搜索的中心位置 |
| number | radius | Required parameter | 搜索半径 |
| table of string | only_classes? | [] | 仅返回这些类的 Actor(传入空数组以获取所有类) |
| integer | dimension? | 0 | 要搜索的维度(传入 0 以搜索所有维度) |
Returns
| Type | Description |
|---|---|
| table of Base Actor | 在半径内找到的 Actor 列表 |
Moderate

GetAllValuesKeys
获取所有值键的列表
local ret = Server.GetAllValuesKeys()
Optimal

GetCompressionLevel
返回当前使用的服务器压缩级别
local ret = Server.GetCompressionLevel()
Returns
| Type | Description |
|---|---|
| integer | 服务器压缩级别 |
Optimal

GetConnectionCount
返回已连接的玩家数量(包括正在连接过程中的玩家)
local ret = Server.GetConnectionCount()
Returns
| Type | Description |
|---|---|
| integer | No description provided |
Slow

GetCustomSettings
返回作为参数传入或通过新游戏屏幕设置的服务器自定义设置
local ret = Server.GetCustomSettings()
Optimal

GetDefaultEntityDimension
返回默认的实体维度
local ret = Server.GetDefaultEntityDimension()
Returns
| Type | Description |
|---|---|
| integer | No description provided |
See also SetDefaultEntityDimension.
Optimal

GetDefaultPlayerDimension
返回默认的玩家维度
local ret = Server.GetDefaultPlayerDimension()
Returns
| Type | Description |
|---|---|
| integer | No description provided |
See also SetDefaultPlayerDimension.
Optimal

GetDescription
返回服务器描述
local ret = Server.GetDescription()
Returns
| Type | Description |
|---|---|
| string | 服务器描述 |
See also SetDescription.
Moderate

GetEntityByID
返回指定 ID 的实体
local ret = Server.GetEntityByID(entity_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | entity_id | Required parameter | No description provided |
Returns
| Type | Description |
|---|---|
| Base Entity or nil | 实体 |
Optimal

GetIP
返回服务器的公网 IP
local ret = Server.GetIP()
Returns
| Type | Description |
|---|---|
| string | 服务器公网 IP |
Optimal

GetLogLevel
返回当前的日志级别
local ret = Server.GetLogLevel()
Returns
| Type | Description |
|---|---|
| integer | 日志级别 |
Optimal

GetMap
返回当前地图的包名称
local ret = Server.GetMap()
Returns
| Type | Description |
|---|---|
| string | 当前地图包 |
Optimal

GetMapAsset
返回当前地图资产的引用路径
local ret = Server.GetMapAsset()
Returns
| Type | Description |
|---|---|
| string | 当前地图资产 |
Slow

GetMapConfig
返回当前地图的自定义配置列表(存储在 MAP_NAME.toml 中)
local ret = Server.GetMapConfig()
Returns
| Type | Description |
|---|---|
| table | 当前地图的自定义配置列表 |
Moderate

GetMapSpawnPoints
返回当前地图的生成点列表(存储在 MAP_NAME.toml 中)
local ret = Server.GetMapSpawnPoints()
See also AddMapSpawnPoint.
Optimal

GetMaxPlayers
返回配置的最大玩家数
local ret = Server.GetMaxPlayers()
Returns
| Type | Description |
|---|---|
| integer | 配置的最大玩家数 |
See also SetMaxPlayers.
Optimal

GetName
返回服务器名称
local ret = Server.GetName()
Returns
| Type | Description |
|---|---|
| string | 服务器名称 |
See also SetName.
Slow

GetPackages
返回正在运行的包列表,也可以选择返回服务器中安装的所有包
local ret = Server.GetPackages(only_loaded?, package_type_filter?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| boolean | only_loaded? | true | 设为 true 时,该函数仅返回已加载且正在运行的包。注意:设为 false 将从磁盘读取包列表,这是一项耗时的慢速操作! |
| PackageType | package_type_filter? | PackageType.All | 要返回的包类型。默认返回所有类型。 |
Moderate

GetPlayersInRadius
返回指定半径内的玩家
local ret = Server.GetPlayersInRadius(location, radius, dimension?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| Vector | location | Required parameter | 搜索的中心位置 |
| number | radius | Required parameter | 搜索半径 |
| integer | dimension? | 0 | 要搜索的维度 |
Optimal

GetPort
返回服务器端口
local ret = Server.GetPort()
Returns
| Type | Description |
|---|---|
| integer | 服务器端口 |
Optimal

GetQueryPort
返回服务器 QueryPort
local ret = Server.GetQueryPort()
Returns
| Type | Description |
|---|---|
| integer | 服务器 QueryPort |
Optimal

GetTickRate
返回配置的刻速率
local ret = Server.GetTickRate()
Returns
| Type | Description |
|---|---|
| integer | 服务器刻速率 |
Optimal

GetTime
获取毫秒级 Unix 时间戳
local ret = Server.GetTime()
Returns
| Type | Description |
|---|---|
| integer | unix 时间戳 |
Moderate

GetValue
获取指定键的值
local ret = Server.GetValue(key, fallback)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | key | Required parameter | 键 |
| any | fallback | Required parameter | 当键不存在时的备用值 |
Returns
| Type | Description |
|---|---|
| any | 键对应的值,若键不存在则返回备用值 |
See also GetAllValuesKeys, SetValue.
Optimal

GetVersion
返回服务器版本
local ret = Server.GetVersion()
Returns
| Type | Description |
|---|---|
| string | 服务器版本 |
Optimal

IsAnnounced
返回服务器是否已发布到主列表中
local ret = Server.IsAnnounced()
Returns
| Type | Description |
|---|---|
| boolean | 服务器是否已发布到主列表中 |
Optimal

IsPackageLoaded
返回指定的包当前是否已加载
local ret = Server.IsPackageLoaded(package_name)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | package_name | Required parameter | No description provided |
Returns
| Type | Description |
|---|---|
| boolean | 包是否已加载 |
Moderate

KickByAccountID
通过玩家的账户 ID 将其从服务器踢出,非常适合在 PlayerConnect 事件内部调用
Server.KickByAccountID(player_account_id, reason)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | player_account_id | Required parameter | No description provided |
| string | reason | Required parameter | No description provided |
Slow

LoadPackage
加载一个包
local ret = Server.LoadPackage(package_folder_name)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | package_folder_name | Required parameter | 要加载的包 |
Returns
| Type | Description |
|---|---|
| boolean | 包是否成功加载 |
See also ReloadPackage, UnloadPackage.
Slow

ReloadPackage
重新加载一个包
注:该包实际上会在下一个服务器刻中被重新加载
local ret = Server.ReloadPackage(package_folder_name)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | package_folder_name | Required parameter | 要重新加载的包 |
Returns
| Type | Description |
|---|---|
| boolean | 是否找到了该包 |
See also LoadPackage, UnloadPackage.
Slow

Restart
重启服务器,重新加载所有包并重新连接所有玩家
Server.Restart()
Optimal

SetDefaultEntityDimension
设置实体生成时的默认服务器维度。你可以在生成实体前更改它,并在生成后将其恢复,以便直接将特定实体生成到指定维度中
Server.SetDefaultEntityDimension(default_dimension)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | default_dimension | Required parameter | No description provided |
See also GetDefaultEntityDimension.
Optimal

SetDefaultPlayerDimension
设置玩家生成时的默认服务器维度
Server.SetDefaultPlayerDimension(default_dimension)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | default_dimension | Required parameter | No description provided |
See also GetDefaultPlayerDimension.
Optimal

SetDescription
设置服务器的描述,可选择是否覆盖 Config.toml
Server.SetDescription(description, persist_to_config_file?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | description | Required parameter | 新的描述 |
| boolean | persist_to_config_file? | false | 是否持久化保存到配置文件 |
See also GetDescription.
Optimal

SetLogo
设置服务器的徽标,可选择是否覆盖 Config.toml
Server.SetLogo(logo_url, persist_to_config_file?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | logo_url | Required parameter | 新的徽标 |
| boolean | persist_to_config_file? | false | 是否持久化保存到配置文件 |
Optimal

SetMaxPlayers
设置服务器的最大玩家槽位数,可选择是否覆盖 Config.toml
Server.SetMaxPlayers(max_players, persist_to_config_file?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | max_players | Required parameter | 最大玩家数 |
| boolean | persist_to_config_file? | false | 是否持久化保存到配置文件 |
See also GetMaxPlayers.
Optimal

SetName
设置服务器的名称,可选择是否覆盖 Config.toml
Server.SetName(name, persist_to_config_file?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | name | Required parameter | 新的名称 |
| boolean | persist_to_config_file? | false | 是否持久化保存到配置文件 |
See also GetName.
Optimal

SetPassword
设置服务器的密码,可选择是否覆盖 Config.toml
Server.SetPassword(password, persist_to_config_file?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | password | Required parameter | 新的密码 |
| boolean | persist_to_config_file? | false | 是否持久化保存到配置文件 |
Moderate

SetValue
在服务器端设置一个全局值,可以从任何地方访问(服务器端)
Server.SetValue(key, value, sync_on_client?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | key | Required parameter | 键 |
| any | value | Required parameter | 明度 |
| boolean | sync_on_client? | false | 如果启用,将把此值同步到所有客户端,可通过静态类 Client.GetValue() 进行访问 |
See also GetAllValuesKeys, GetValue, ValueChange.
Slow

Stop
停止服务器
Server.Stop()
Optimal

Subscribe
订阅事件
local ret = Server.Subscribe(event_name, function)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | event_name | Required parameter | 事件的名称 |
| function | function | Required parameter | 回调函数 |
Returns
| Type | Description |
|---|---|
| function | 函数回调本身 |
Moderate

Unban
通过账户 ID 解封玩家
Server.Unban(account_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | account_id | Required parameter | nanos 账户 ID |
Slow

UnloadPackage
卸载一个包
注:该包实际上会在下一个服务器刻中被卸载
local ret = Server.UnloadPackage(package_folder_name)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | package_folder_name | Required parameter | 你想卸载的包文件夹名称 |
Returns
| Type | Description |
|---|---|
| boolean | 是否找到了该包 |
See also LoadPackage, ReloadPackage.
Optimal

Unsubscribe
退订此特定类和此特定包中所有已订阅的事件,可选择传递函数以仅退订该回调
Server.Unsubscribe(event_name, function?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | event_name | Required parameter | 事件的名称 |
| function | function? | nil | 回调函数 |
🚀 事件
| Name | Description | |
|---|---|---|
ChangeMap | 当地图更改时调用 | |
PlayerConnect | 当玩家尝试连接到服务器时调用 | |
PlayerDisconnect | 当玩家断开与服务器的连接时调用 | |
Restart | 服务器已重启 | |
Start | 服务器已启动 | |
Stop | 服务器已停止 | |
Tick | 默认每 33 毫秒触发一次 | |
ValueChange | 当通过 Server.SetValue() 更改值时触发 |

ChangeMap
当地图更改时调用。该事件总是在 Restart 事件触发前紧接着调用
Server.Subscribe("ChangeMap", function(old_map, new_map)
-- ChangeMap was called
end)
Arguments
| Type | Argument | Description |
|---|---|---|
| string | old_map | No description provided |
| string | new_map | No description provided |

PlayerConnect
当玩家尝试连接到服务器时调用
在此事件内部,可以使用 Server.KickByAccountID() 或 Server.BanByAccountID() 来踢出或封禁玩家。
Server.Subscribe("PlayerConnect", function(IP, player_account_ID, player_name, player_steam_ID)
-- PlayerConnect was called
end)
Arguments
| Type | Argument | Description |
|---|---|---|
| string | IP | 玩家的 IP |
| string | player_account_ID | 玩家的账户 ID |
| string | player_name | 玩家名称 |
| string | player_steam_ID | 玩家的 SteamID |
See also KickByAccountID, BanByAccountID, PlayerDisconnect.

PlayerDisconnect
当玩家断开与服务器的连接时调用
Server.Subscribe("PlayerDisconnect", function(IP, player_account_ID, player_name, player_steam_ID, disconnect_reason)
-- PlayerDisconnect was called
end)
Arguments
| Type | Argument | Description |
|---|---|---|
| string | IP | 玩家的 IP |
| string | player_account_ID | 玩家的账户 ID |
| string | player_name | 玩家名称 |
| string | player_steam_ID | 玩家的 SteamID |
| string | disconnect_reason | 断开连接的原因 |
See also PlayerConnect.

Restart
服务器已重启
Server.Subscribe("Restart", function()
-- Restart was called
end)

Start
服务器已启动
Server.Subscribe("Start", function()
-- Start was called
end)

Stop
服务器已停止
Server.Subscribe("Stop", function()
-- Stop was called
end)

Tick
默认每 33 毫秒触发一次
注意:此处应该只执行小型操作,否则可能导致服务器卡顿和延迟
Server.Subscribe("Tick", function(delta_time)
-- Tick was called
end)
Arguments
| Type | Argument | Description |
|---|---|---|
| float | delta_time | No description provided |

ValueChange
当通过 Server.SetValue() 更改值时触发Server.Subscribe("ValueChange", function(key, value)
-- ValueChange was called
end)
See also SetValue.


