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
DeregisterScriptAnalysisCallback
FindScriptDocument
Tham Số
Lợi Nhuận
Mẫu mã
ScriptDocument:CloseAsync
--!nocheck
-- Chạy đoạn 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 đừ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 chế độ xem Output đang mở
2. Chạy mã dưới đây 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 giả 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ậ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 đoạn 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ã
OpenScriptDocumentAsync()
--!nocheck
-- Chạy đoạn mã sau trong Command Bar
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 script")
else
print(`Không thể mở tài liệu script: {err}`)
endRegisterAutocompleteCallback
Lợi Nhuận
()
Mẫu mã
Đăng ký Callback Tự động hoàn thành() và Hủy đăng ký Callback Tự động hoàn thành()
--!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)
-- Để hủy đăng ký callback, chạy đoạn mã sau trong Command Bar
ScriptEditorService:DeregisterAutocompleteCallback("foo")RegisterScriptAnalysisCallback
Lợi Nhuận
()
Mẫu mã
RegisterScriptAnalysisCallback()
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 = "Đã tìm thấy Foo ở đâ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
()
Mẫu mã
Cập nhật Nguồn Không Đồng Bộ
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService:UpdateSourceAsync(workspace.Script, function(oldContent)
return oldContent .. " Thế Giới!"
end)Sự Kiện
TextDocumentDidChange
ScriptEditorService.TextDocumentDidChange(
Tham Số
changesArray:Variant |
Mẫu mã
ScriptEditorService.TextDocumentDidChange
--!nocheck
-- Chạy đoạn mã sau trong Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidChange:Connect(function(scriptDocument, changes)
print("Đã thay đổi", scriptDocument, changes)
end)TextDocumentDidClose
Tham Số
Mẫu mã
ScriptEditorService.TextDocumentDidClose
--!nocheck
-- Chạy đoạn mã sau trong Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidClose:Connect(function(scriptDocument)
print("Đã đóng", scriptDocument)
end)TextDocumentDidOpen
Tham Số
Mẫu mã
ScriptEditorService.TextDocumentDidOpen
--!nocheck
-- Chạy đoạn mã sau trong Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidOpen:Connect(function(scriptDocument)
print("Đã mở", scriptDocument)
end)