Kelas Engine
ScriptEditorService
*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.
Rangkuman
Metode
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):() |
Acara
TextDocumentDidChange(document: ScriptDocument,changesArray: Variant):RBXScriptSignal |
TextDocumentDidClose(oldDocument: ScriptDocument):RBXScriptSignal |
TextDocumentDidOpen(newDocument: ScriptDocument):RBXScriptSignal |
Referensi API
Metode
DeregisterAutocompleteCallback
Parameter
Memberikan nilai
()
Contoh Kode
DeregisterAutocompleteCallback()
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Menghapus callback autocompletion untuk "foo"
ScriptEditorService:DeregisterAutocompleteCallback("foo")DeregisterScriptAnalysisCallback
FindScriptDocument
Parameter
Memberikan nilai
Contoh Kode
ScriptDocument:TutupAsync
--!nocheck
-- Jalankan kode berikut di Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- Temukan dokumen skrip yang terbuka pertama
for _, document in documents do
-- Command Bar tidak bisa ditutup, jadi jangan pilih itu
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`Tutup {scriptDocument.Name}`)
else
warn(`Gagal untuk menutup {scriptDocument.Name} karena: {err}`)
end
else
print("Tidak ada skrip yang terbuka")
endMenghubungkan ke ScriptDocument.ViewportChanged
--!nocheck
--[[
Untuk menjalankan:
1. Pastikan tampilan Output terbuka
2. Jalankan kode di bawah ini di Command Bar
3. Gulir ke atas dan ke bawah di jendela Skrip yang terbuka
Pernyataan print dari peristiwa ViewportChanged akan muncul di Output
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Buat teks yang membentang banyak baris
local dummyText = string.rep("-- Dummy Text\n", 60)
-- Buat skrip yang berisi teks dummy dan buka
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`Gagal membuka skrip karena: {err}`)
return
end
-- Dapatkan referensi ke skrip yang dibuka
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`Viewport Skrip Berubah - startLine: {startLine}, endLine: {endLine}`)
end
-- Hubungkan peristiwa ViewportChanged ke fungsi di atas yang mencetak garis awal dan akhir dari viewport yang diperbarui
scriptDocument.ViewportChanged:Connect(onViewportChanged)GetEditorSource
Parameter
Memberikan nilai
GetScriptDocuments
Memberikan nilai
Contoh Kode
Cetak nama setiap skrip
--!nocheck
-- Jalankan kode berikut di Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local scriptDocuments = ScriptEditorService:GetScriptDocuments()
for _, scriptDocument in scriptDocuments do
-- Mencetak nama setiap skrip
if not scriptDocument:IsCommandBar() then
print(scriptDocument.Name)
end
endOpenScriptDocumentAsync
Parameter
| Nilai Default: "nil" |
Memberikan nilai
Contoh Kode
BukaDokumenScriptAsync()
--!nocheck
-- Jalankan kode berikut di 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("Dokumen skrip dibuka")
else
print(`Gagal membuka dokumen skrip: {err}`)
endRegisterAutocompleteCallback
Memberikan nilai
()
Contoh Kode
RegisterAutocompleteCallback() dan DeregisterAutocompleteCallback()
--!nocheck
-- Jalankan kode berikut di 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)
-- Untuk membatalkan pendaftaran callback, jalankan kode berikut di Command Bar
ScriptEditorService:DeregisterAutocompleteCallback("foo")RegisterScriptAnalysisCallback
Memberikan nilai
()
Contoh Kode
DaftarCallbackAnalisisSkrip()
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
-- Iterasi baris demi baris
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 ditemukan di sini!",
severity = Enum.Severity.Warning,
})
end
lineNo = lineNo + #newline:gsub("\n+", "\0%0\0"):gsub(".%z.", "."):gsub("%z", "")
end
return response
end)UpdateSourceAsync
Parameter
Memberikan nilai
()
Acara
TextDocumentDidChange
ScriptEditorService.TextDocumentDidChange(
Parameter
changesArray:Variant |
Contoh Kode
ScriptEditorService.TeksDokumenTelahBerubah
--!nocheck
-- Jalankan kode berikut di Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidChange:Connect(function(scriptDocument, changes)
print("Mengubah", scriptDocument, changes)
end)TextDocumentDidClose
Parameter
Contoh Kode
ScriptEditorService.TextDocumentDidClose
--!nocheck
-- Jalankan kode berikut di Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidClose:Connect(function(scriptDocument)
print("Tutup", scriptDocument)
end)TextDocumentDidOpen
Parameter
Contoh Kode
ScriptEditorService.TextDocumentDidOpen
--!nocheck
-- Jalankan kode berikut di Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidOpen:Connect(function(scriptDocument)
print("Terbuka", scriptDocument)
end)