ScriptEditorService
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
Ta usługa jest używana do wchodzenia w interakcję z instancjami ScriptDocument.
Podsumowanie
Metody
Usuwa wcześniej zarejestrowany powrót z nazwą name.
Usuwa wcześniej zarejestrowany powrót z nazwą name.
Zwraca otwarte ScriptDocument odpowiadające danemu LuaSourceContainer lub nil jeśli dany skrypt nie jest otwarty.
Zwraca źródło czasu edycji dla podanego skryptu.
Zwraca tablicę otwartych obecnie dokumentów skryptowych, w tym pasek poleceń.
Rejestruje powrotną funkcję autouzupełniania callbackFunction o nazwie name z priorytetem priority.
Rejestruje wezwanie do analizy skryptu callbackFunction o nazwie name z priority.
Żądania, w których edytor skryptów otwiera określony skrypt.Zwraca (true, nil), jeśli wniosek się powiedzie.Zwraca (false, string) w przypadku niepowodzenia żądania, z ciągiem opisującym problem.
Tworzy nową zawartość z starego skryptu i aktualizuje edytor skryptów, jeśli jest otwarty, lub instancję Script, jeśli edytor skryptów jest zamknięty.
Zdarzenia
Występuje tuż po zmianach ScriptDocument po zmianach.
Pożary występują tuż przed zniszczeniem obiektu ScriptDocument, które następuje tuż po zamknięciu edytora skryptów.
Ogień pojawia się tuż po utworzeniu i przypisaniu obiektu ScriptDocument do usługi, co zdarza się tuż po otwarciu edytora skryptów.
Właściwości
Metody
DeregisterAutocompleteCallback
Parametry
Zwroty
Przykłady kodu
game.ScriptEditorService:DeregisterAutocompleteCallback("foo")
DeregisterScriptAnalysisCallback
Parametry
Zwroty
Przykłady kodu
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService:DeregisterScriptAnalysisCallback("foo")
FindScriptDocument
Parametry
Zwroty
Przykłady kodu
--!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
Zwroty
Przykłady kodu
--!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
Parametry
Zwroty
Przykłady kodu
--!noczekuj
-- Wykonaj następujący kod w pasku poleceń
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)
-- Aby odrejestrować powiadomienie o wywołaniu, wykonaj następujący kod w pasku poleceń
ScriptEditorService:DeregisterAutocompleteCallback("foo")
RegisterScriptAnalysisCallback
Parametry
Zwroty
Przykłady kodu
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
-- Powtarzaj linię po linii
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
Parametry
Zwroty
Przykłady kodu
--!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
Parametry
Zwroty
Zdarzenia
TextDocumentDidChange
Parametry
Przykłady kodu
--!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
Parametry
Przykłady kodu
--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidClose:Connect(function(scriptDocument)
print("Closed", scriptDocument)
end)
TextDocumentDidOpen
Parametry
Przykłady kodu
--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidOpen:Connect(function(scriptDocument)
print("Opened", scriptDocument)
end)