debug

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处。


摘要


API 参考
职能
traceback
debug.traceback(
message:string, level:number
参数
message:string
level:number
默认值:1
返回
代码示例
调试回溯

traceback
debug.traceback(
thread:coroutine, message:string, level:number
参数
thread:coroutine
message:string
level:number
默认值:1
返回
代码示例
线程的调试回溯

info
debug.info(
level:number, options:string
参数
level:number
options:string
返回
代码示例
按级别调试信息

info
debug.info(
function:function, options:string
参数
function:function
options:string
返回
代码示例
按函数调试信息

info
debug.info(
thread:coroutine, level:number, options:string
参数
thread:coroutine
level:number
options:string
返回
代码示例
线程调试信息

profilebegin
debug.profilebegin(label:string):()
参数
label:string
返回
()
代码示例
调试配置开始
local function buildGrid(size)
debug.profilebegin("BuildGrid")
local grid = table.create(size * size, 0)
debug.profileend()
return grid
end
print(#buildGrid(32)) --> 1024

profileend
debug.profileend():()
返回
()
代码示例
调试配置结束
debug.profilebegin("Outer")
debug.profilebegin("Inner")
-- 此处的工作在两个标签下计时
debug.profileend() -- 关闭 "Inner"
debug.profileend() -- 关闭 "Outer"

getmemorycategory
debug.getmemorycategory():string
返回
代码示例
调试获取内存类别
local category = debug.getmemorycategory()
print(type(category)) --> string
-- 类别跟随线程,因此新的类别可能与此不同
local previous = debug.setmemorycategory("Pathfinding")
print(debug.getmemorycategory()) --> Pathfinding
debug.setmemorycategory(previous)

setmemorycategory
debug.setmemorycategory(tag:string):string
参数
tag:string
返回
代码示例
调试设置内存类别
local original = debug.setmemorycategory("MapGeneration")
print(debug.getmemorycategory()) --> MapGeneration
-- 在这里进行的分配会在开发者控制台的 "MapGeneration" 下列出
local tiles = table.create(1000, 0)
-- 返回值是之前的类别,因此可以恢复
print(debug.setmemorycategory(original) == "MapGeneration") --> true
print(#tiles) --> 1000

resetmemorycategory
debug.resetmemorycategory():()
返回
()
代码示例
调试重置内存类别
local automatic = debug.getmemorycategory()
debug.setmemorycategory("生成中")
print(debug.getmemorycategory()) --> 生成中
debug.resetmemorycategory()
print(debug.getmemorycategory() == automatic) --> true

dumpcodesize
debug.dumpcodesize():()
返回
()
代码示例
调试转储代码大小
-- debug.dumpcodesize() 仅在 Studio 命令栏中有效,它会打印一个
-- 报告到输出中,每个本机脚本一个表,最大的函数优先:
-- # 本机代码大小分析
-- <n> 个脚本,<n> 个函数,<n> KB 内存,256 MB 代码大小限制
-- ## <script name>
-- <n> 个函数,<n> KB 内存
-- | 函数 | 行 | 本机代码大小 | 本机代码大小 (%) |
-- 只有以 --!native 开头并已运行的脚本会出现在报告中。
-- 从脚本中会引发错误,pcall 会捕获
local ok, message = pcall(debug.dumpcodesize)
print(ok) --> false
print(message) --> dumpcodesize 只能从 CommandBar 调用

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。