Lớp công cụ
ScriptEditorService
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm Tắt
Phương Pháp
DeregisterAutocompleteCallback(name: string):() |
DeregisterScriptAnalysisCallback(name: string):() |
GetEditorSource(script: LuaSourceContainer):string |
OpenScriptDocumentAsync(script: LuaSourceContainer,options: Dictionary):Tuple |
RegisterAutocompleteCallback(name: string,priority: number,callbackFunction: function):() |
RegisterScriptAnalysisCallback(name: string,priority: number,callbackFunction: function):() |
UpdateSourceAsync(script: LuaSourceContainer,callback: function):() |
Sự Kiện
TextDocumentDidChange(document: ScriptDocument,changesArray: Variant):RBXScriptSignal |
TextDocumentDidClose(oldDocument: ScriptDocument):RBXScriptSignal |
TextDocumentDidOpen(newDocument: ScriptDocument):RBXScriptSignal |
Tài Liệu Tham Khảo API
Phương Pháp
DeregisterAutocompleteCallback
Tham Số
Lợi Nhuận
()
Mẫu mã
Hủy Đăng Ký Callback Tự Động Hoàn Thành()
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Hủy đăng ký callback tự động hoàn thành cho "foo"
ScriptEditorService:DeregisterAutocompleteCallback("foo")DeregisterScriptAnalysisCallback
FindScriptDocument
Tham Số
Lợi Nhuận
Mẫu mã
ScriptDocument:CloseAsync
--!nocheck
-- Chạy mã sau trong Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- Tìm tài liệu script mở đầu tiên
for _, document in documents do
-- Command Bar không thể bị đóng, vì vậy không chọn nó
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`Đã đóng {scriptDocument.Name}`)
else
warn(`Không thể đóng {scriptDocument.Name} vì: {err}`)
end
else
print("Không có script nào đang mở")
endKết nối với ScriptDocument.ViewportChanged
--!nocheck
--[[
Để chạy:
1. Đảm bảo rằng khung Output đang mở
2. Chạy đoạn mã bên dưới trong Command Bar
3. Cuộn lên và xuống trong cửa sổ Script đã mở
Các câu lệnh Print từ sự kiện ViewportChanged sẽ xuất hiện trong Output
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Tạo văn bản trải dài nhiều dòng
local dummyText = string.rep("-- Dummy Text\n", 60)
-- Tạo một script chứa văn bản dummy và mở nó
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`Không thể mở script vì: {err}`)
return
end
-- Lấy tham chiếu đến script đã mở
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`Viewport Script Đã Thay Đổi - startLine: {startLine}, endLine: {endLine}`)
end
-- Kết nối sự kiện ViewportChanged với hàm ở trên để in ra dòng bắt đầu và kết thúc của viewport được cập nhật
scriptDocument.ViewportChanged:Connect(onViewportChanged)GetEditorSource
Tham Số
Lợi Nhuận
GetScriptDocuments
Lợi Nhuận
Mẫu mã
In danh sách tên của mọi script
--!nocheck
-- Chạy mã sau trong Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local scriptDocuments = ScriptEditorService:GetScriptDocuments()
for _, scriptDocument in scriptDocuments do
-- In tên của mỗi script
if not scriptDocument:IsCommandBar() then
print(scriptDocument.Name)
end
endOpenScriptDocumentAsync
Tham Số
| Giá Trị Mặc Định: "nil" |
Lợi Nhuận
Mẫu mã
MởTàiLiệuKịchBảnAsync()
--!nocheck
-- Chạy đoạn mã sau trong Thanh Lệnh
local ScriptEditorService = game:GetService("ScriptEditorService")
local Workspace = game:GetService("Workspace")
local newScript = Instance.new("Script")
newScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(newScript)
if success then
print("Đã mở tài liệu kịch bản")
else
print(`Không thể mở tài liệu kịch bản: {err}`)
endRegisterAutocompleteCallback
Lợi Nhuận
()
Mẫu mã
Đăng kýCallbackTựĐộngHoànTất() và HuỷĐăngKýCallbackTựĐộngHoànTất()
--!nocheck
-- Chạy đoạn mã sau trong Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
type Request = {
position: {
line: number,
character: number,
},
textDocument: {
document: ScriptDocument?,
script: LuaSourceContainer?,
},
}
type Response = {
items: {
{
label: string,
kind: Enum.CompletionItemKind?,
tags: { Enum.CompletionItemTag }?,
detail: string?,
documentation: {
value: string,
}?,
overloads: number?,
learnMoreLink: string?,
codeSample: string?,
preselect: boolean?,
textEdit: {
newText: string,
replace: {
start: { line: number, character: number },
["end"]: { line: number, character: number },
},
}?,
}
},
}
local autocompleteCallback = function(request: Request, response: Response): Response
local item = {
label = "foo",
preselect = true,
}
table.insert(response.items, item)
return response
end
ScriptEditorService:RegisterAutocompleteCallback("foo", 1, autocompleteCallback)
-- Để huỷ đăng ký callback, hãy chạy đoạn mã sau trong Command Bar
ScriptEditorService:DeregisterAutocompleteCallback("foo")RegisterScriptAnalysisCallback
Lợi Nhuận
()
Mẫu mã
ĐăngKýPhânTíchScriptCallback()
type Request = {
["script"]: LuaSourceContainer,
}
type Response = {
diagnostics: {
{
range: {
start: {
line: number,
character: number,
},
["end"]: {
line: number,
character: number,
},
},
code: string?,
message: string,
severity: Enum.Severity?,
codeDescription: { href: string }?,
}
},
}
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService:RegisterScriptAnalysisCallback("foo", 1, function(Req: Request): Response
local response = {
diagnostics = {},
}
local lineNo = 1
-- Lặp qua từng dòng
for text, newline in Req.script.Source:gmatch("([^\r\n]*)([\r\n]*)") do
local startIndex, endIndex = string.find(text, "Foo")
if startIndex and endIndex then
table.insert(response.diagnostics, {
range = {
["start"] = {
line = lineNo,
character = startIndex,
},
["end"] = {
line = lineNo,
character = endIndex,
},
},
code = "FooFinder",
message = "Foo được tìm thấy ở đây!",
severity = Enum.Severity.Warning,
})
end
lineNo = lineNo + #newline:gsub("\n+", "\\0%0\\0"):gsub(".%z.", "."):gsub("%z", "")
end
return response
end)UpdateSourceAsync
Tham Số
Lợi Nhuận
()
Sự Kiện
TextDocumentDidChange
ScriptEditorService.TextDocumentDidChange(
Tham Số
changesArray:Variant |
Mẫu mã
ScriptEditorService.TextDocumentDidChange
--!nocheck
-- Chạy đoạn mã sau trong thanh lệnh
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidChange:Connect(function(scriptDocument, changes)
print("Đã thay đổi", scriptDocument, changes)
end)TextDocumentDidClose
Tham Số
Mẫu mã
Dịch vụChỉnh sửaKịch bản.Tài liệuKịch bảnĐãĐóng
--!nocheck
-- Chạy mã sau trong Thanh Lệnh
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidClose:Connect(function(scriptDocument)
print("Đã Đóng", scriptDocument)
end)TextDocumentDidOpen
Tham Số
Mẫu mã
Dịch vụ Chỉnh sửa Kịch bản.Tài liệu Kịch bản Đã Mở
`--!nocheck`
`-- Chạy đoạn mã sau trong Command Bar`
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidOpen:Connect(function(scriptDocument)
print("Đã mở", scriptDocument)
end)