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

🔤 控制台

提供注册控制台命令和记录日志消息的功能。

🗿Static ClassClient & Server🧑‍💻API Source

🎒 示例​

(Console.Log) Basic
Console.Log("Hello, world!")
-- Outputs "Hello, world!"
(Console.Log) With formatting (string)
local player_name = "John"
Console.Log("Hello %s!", player_name)
-- Outputs "Hello John!"
(Console.Log) With formatting (integer)
local health = 50
local max_health = 100
Console.Log("Player health: %d/%d", health, max_health)
-- Outputs "Player health: 50/100"
(Console.RegisterCommand) Registering to a command that sends a message to everyone
Console.RegisterCommand("hello", function(text)
Console.Log("Sending a 'Hello %s' to everyone!", text)
Chat.BroadcastMessage("Hello " .. text)
end, "says a message to everyone", { "my_text" })
(Console.RunCommand) Running a Console Command
Console.RunCommand("hello")
(Console "PlayerSubmit" Event) Handles Console Inputs
Console.Subscribe("PlayerSubmit", function(text)
if (text == "some_text") then
Console.Log("Player submitted some_text!")
end
end)

🗿 静态函数​

ReturnsNameDescription
Debug在控制台中记录一条调试消息,支持参数格式化。仅当当前日志级别为 Debug 或 Verbose 时输出
Error在控制台中记录一条带有堆栈信息的红色错误消息
Log在控制台中记录并格式化一条消息
RegisterCommand注册一个新的控制台命令
RunCommand通过编程方式运行一个控制台命令(只有通过脚本注册的命令才能被触发)
functionSubscribe订阅事件
Unsubscribe退订此特定类和此特定包中所有已订阅的事件,可选择传递函数以仅退订该回调
Warn在控制台中记录一条带有堆栈信息的橙色警告消息

Optimal

Debug​

在控制台中记录一条调试消息,支持参数格式化。仅当当前日志级别为 Debug 或 Verbose 时输出
Console.Debug(message, args...?)

Parameters

TypeParameterDefaultDescription
stringmessage Required parameter 要打印的消息
anyargs...?nil
其他用于通过 string.format 格式化消息的参数

Optimal

Error​

在控制台中记录一条带有堆栈信息的红色错误消息,支持参数格式化
Console.Error(message, args...?)

Parameters

TypeParameterDefaultDescription
stringmessage Required parameter 要打印的消息
anyargs...?nil
其他用于通过 string.format 格式化消息的参数

Optimal

Log​

在控制台中记录并格式化一条消息,支持参数格式化
Console.Log(message, args...?)

Parameters

TypeParameterDefaultDescription
stringmessage Required parameter 要打印的消息
anyargs...?nil
其他用于通过 string.format 格式化消息的参数
Console.Log Examples
Basic
Console.Log("Hello, world!")
-- Outputs "Hello, world!"
With formatting (string)
local player_name = "John"
Console.Log("Hello %s!", player_name)
-- Outputs "Hello John!"
With formatting (integer)
local health = 50
local max_health = 100
Console.Log("Player health: %d/%d", health, max_health)
-- Outputs "Player health: 50/100"

Optimal

RegisterCommand​

注册一个新的控制台命令
Console.RegisterCommand(command, callback, description?, parameters?)

Parameters

TypeParameterDefaultDescription
stringcommand Required parameter 命令
functioncallback Required parameter 当输入该命令时需要调用的回调函数 with this format
stringdescription?
要在控制台中显示的命令描述
table of stringparameters?{}
要在控制台中显示的受支持参数列表
Console.RegisterCommand Examples
Registering to a command that sends a message to everyone
Console.RegisterCommand("hello", function(text)
Console.Log("Sending a 'Hello %s' to everyone!", text)
Chat.BroadcastMessage("Hello " .. text)
end, "says a message to everyone", { "my_text" })

Moderate

RunCommand​

通过编程方式运行一个控制台命令(只有通过脚本注册的命令才能被触发)
Console.RunCommand(command)

Parameters

TypeParameterDefaultDescription
stringcommand Required parameter 命令
Console.RunCommand Examples
Running a Console Command
Console.RunCommand("hello")

Optimal

Subscribe​

订阅事件
local ret = Console.Subscribe(event_name, callback)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 要订阅的事件
functioncallback Required parameter 事件发生时运行的回调函数

Returns

TypeDescription
function函数回调

Optimal

Unsubscribe​

退订此特定类和此特定包中所有已订阅的事件,可选择传递函数以仅退订该回调
Console.Unsubscribe(event_name, callback?)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 要退订的事件
functioncallback?nil
用于专门退订的可选回调

Optimal

Warn​

在控制台中记录一条带有堆栈信息的橙色警告消息,支持参数格式化
Console.Warn(message, args...?)

Parameters

TypeParameterDefaultDescription
stringmessage Required parameter 要打印的消息
anyargs...?nil
其他用于通过 string.format 格式化消息的参数

🚀 事件​

NameDescription
LogEntry当收到一条日志时调用
PlayerSubmit当提交控制台命令时调用

LogEntry​

当收到一条日志时调用
Console.Subscribe("LogEntry", function(text, type)
-- LogEntry was called
end)

Arguments

TypeArgumentDescription
stringtext日志消息
LogTypetype日志的类型

PlayerSubmit​

当提交控制台命令时调用
Console.Subscribe("PlayerSubmit", function(text)
-- PlayerSubmit was called
end)

Arguments

TypeArgumentDescription
stringtext提交的文本
Console "PlayerSubmit" Event Examples
Handles Console Inputs
Console.Subscribe("PlayerSubmit", function(text)
if (text == "some_text") then
Console.Log("Player submitted some_text!")
end
end)