ScriptEditorService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
บริการนี้ใช้สำหรับการโต้ตอบกับตัวอย่าง ScriptDocument
สรุป
วิธีการ
ลบคอลเลกชันที่ลงทะเบียนไว้ก่อนหน้านี้ที่มีชื่อ name
ลบคอลเลกชันที่ลงทะเบียนไว้ก่อนหน้านี้ที่มีชื่อ name
คืนค่าเปิด ScriptDocument ที่ตรงกับที่ให้ไว้ LuaSourceContainer หรือ nil หากสคริปต์ที่ให้ไว้ไม่เปิด
ส่งคืนแหล่งเวลาแก้ไขสำหรับสคริปต์ที่กำหนด
ส่งคืนคอลเลกชันของเอกสารสคริปต์ที่เปิดอยู่ในปัจจุบันรวมถึงแถบคําสั่ง
ลงทะเบียนการโทรกลับอัตโนมัติสำเร็จ callbackFunction ชื่อ name ด้วยลําดับความสําคัญ priority
ลงทะเบียนการโทรกลับการวิเคราะห์สคริปต์ callbackFunction ชื่อ name ด้วย priority
คำขอที่เครื่องมือตัวเขียนสคริปต์เปิดสคริปต์ที่ระบุไว้คืน (จริง, ไม่มี) หากคำขอสําเร็จคืน (false, string) หากคำขอล้มเหลวพร้อมกับสตริงที่อธิบายปัญหา
สร้างเนื้อหาใหม่จากสคริปต์เก่าและอัปเดตตัวแก้ไขสคริปต์หากเปิดหรือ Script ตัวแก้ไขสคริปต์ถ้าปิด
เหตุการณ์
ไฟไหม้เพียงหลังจากการเปลี่ยนแปลง ScriptDocument ครั้ง
ไฟไหม้เพียงก่อนที่วัตถุ ScriptDocument จะถูกทําลายซึ่งเกิดขึ้นทันทีหลังจากที่เครื่องแก้ไขสคริปต์ปิด
ไฟไหม้เพียงหลังจากที่วัตถุ ScriptDocument ถูกสร้างและถูกผูกกับบริการซึ่งเกิดขึ้นทันทีหลังจากที่เครื่องมือตัวแก้ไขสคริปต์เปิด
คุณสมบัติ
วิธีการ
DeregisterAutocompleteCallback
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
game.ScriptEditorService:DeregisterAutocompleteCallback("foo")
DeregisterScriptAnalysisCallback
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService:DeregisterScriptAnalysisCallback("foo")
FindScriptDocument
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- Find the first open script document
for _, document in documents do
-- The Command Bar can't be closed, so don't select it
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`Closed {scriptDocument.Name}`)
else
warn(`Failed to close {scriptDocument.Name} because: {err}`)
end
else
print("No open scripts")
end
--!nocheck
--[[
To run:
1. Ensure Output view is open
2. Run the below code in the Command Bar
3. Scroll up and down in the opened Script window
Print statements from the ViewportChanged event will appear in the Output
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Create text that spans many lines
local dummyText = string.rep("-- Dummy Text\n", 60)
-- Create a script containing the dummy text and open it
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`Failed to open script because: {err}`)
return
end
-- Get a reference to the opened script
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`Script Viewport Changed - startLine: {startLine}, endLine: {endLine}`)
end
-- Connect the ViewportChanged event to the function above that prints the start and end line of the updated viewport
scriptDocument.ViewportChanged:Connect(onViewportChanged)
GetScriptDocuments
ส่งค่ากลับ
ตัวอย่างโค้ด
--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local scriptDocuments = ScriptEditorService:GetScriptDocuments()
for _, scriptDocument in scriptDocuments do
-- Prints the name of each script
if not scriptDocument:IsCommandBar() then
print(scriptDocument.Name)
end
end
RegisterAutocompleteCallback
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
--!โนเช็ค
-- ดำเนินโค้ดต่อไปนี้ในแถบคําสั่ง
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)
-- เพื่อยกเลิกการลงทะเบียนการโทรกลับ ให้รันโค้ดต่อไปนี้ในแถบคําสั่ง
ScriptEditorService:DeregisterAutocompleteCallback("foo")
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
-- ทำซ้ำบรรทัดโดยบรรทัด
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 found here!",
severity = Enum.Severity.Warning,
})
end
lineNo = lineNo + #newline:gsub("\n+", "\0%0\0"):gsub(".%z.", "."):gsub("%z", "")
end
return response
end)
OpenScriptDocumentAsync
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
--!nocheck
-- Run the following code in the 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("Opened script document")
else
print(`Failed to open script document: {err}`)
end
UpdateSourceAsync
พารามิเตอร์
ส่งค่ากลับ
เหตุการณ์
TextDocumentDidChange
พารามิเตอร์
ตัวอย่างโค้ด
--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidChange:Connect(function(scriptDocument, changes)
print("Changed", scriptDocument, changes)
end)
TextDocumentDidClose
พารามิเตอร์
ตัวอย่างโค้ด
--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidClose:Connect(function(scriptDocument)
print("Closed", scriptDocument)
end)
TextDocumentDidOpen
พารามิเตอร์
ตัวอย่างโค้ด
--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidOpen:Connect(function(scriptDocument)
print("Opened", scriptDocument)
end)