Engine-klasse
ScriptEditorService
*Deze inhoud is vertaald met behulp van AI (bèta) en kan fouten bevatten. Klik hier om deze pagina in het Engels te bekijken.
Samenvatting
Methoden
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):() |
Evenementen
TextDocumentDidChange(document: ScriptDocument,changesArray: Variant):RBXScriptSignal |
TextDocumentDidClose(oldDocument: ScriptDocument):RBXScriptSignal |
TextDocumentDidOpen(newDocument: ScriptDocument):RBXScriptSignal |
API-referentie
Methoden
DeregisterAutocompleteCallback
DeregisterScriptAnalysisCallback
FindScriptDocument
Parameters
Teruggaves
Codevoorbeelden
ScriptDocument:CloseAsync
--!nocheck
-- Voer de volgende code uit in de Opdrachtbalk
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- Vind het eerste geopende scriptdocument
for _, document in documents do
-- De Opdrachtbalk kan niet worden gesloten, dus selecteer deze niet
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`Gesloten {scriptDocument.Name}`)
else
warn(`Kon {scriptDocument.Name} niet sluiten omdat: {err}`)
end
else
print("Geen geopende scripts")
endVerbinden met ScriptDocument.ViewportChanged
--!nocheck
--[[
Om te draaien:
1. Zorg ervoor dat de Output-weergave open is
2. Voer de onderstaande code in de Command Bar uit
3. Scroll omhoog en omlaag in het geopende Script-venster
Afdrukverklaringen van het ViewportChanged-evenement verschijnen in de Output
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Maak tekst die over meerdere regels gaat
local dummyText = string.rep("-- Dummy Text\n", 60)
-- Maak een script met de dummytekst en open het
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`Kon script niet openen omdat: {err}`)
return
end
-- Verkrijg een referentie naar het geopende script
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`Script Viewport Changed - startLine: {startLine}, endLine: {endLine}`)
end
-- Verbind het ViewportChanged-evenement met de bovenstaande functie die de start- en eindregel van de bijgewerkte viewport afdrukt
scriptDocument.ViewportChanged:Connect(onViewportChanged)GetEditorSource
Parameters
Teruggaves
GetScriptDocuments
Teruggaves
Codevoorbeelden
Print de naam van elk script
--!nocheck
-- Voer de volgende code uit in de Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local scriptDocuments = ScriptEditorService:GetScriptDocuments()
for _, scriptDocument in scriptDocuments do
-- Print de naam van elk script
if not scriptDocument:IsCommandBar() then
print(scriptDocument.Name)
end
endOpenScriptDocumentAsync
Parameters
| Standaardwaarde: nil |
Teruggaves
Codevoorbeelden
OpenScriptDocumentAsync()
--!nocheck
-- Voer de volgende code uit in de 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("Scriptdocument geopend")
else
print(`Kon scriptdocument niet openen: {err}`)
endOpenScriptDocumentAsync() met InitialReadOnlyReasons
local ScriptEditorService = game:GetService("ScriptEditorService")
local Workspace = game:GetService("Workspace")
local templateScript = Instance.new("Script")
templateScript.Source = "-- Sjabloon geleverd door MyPlugin"
templateScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(templateScript, {
InitialReadOnlyReasons = { "MyPluginTemplateScript" },
})
if success then
local scriptDocument = ScriptEditorService:FindScriptDocument(templateScript)
print(`Geopend alleen-lezen sjabloon: {scriptDocument:IsReadOnly()}`)
else
warn(`Kon scriptdocument niet openen: {err}`)
endRegisterAutocompleteCallback
Teruggaves
()
Codevoorbeelden
RegisterAutocompleteCallback() en DeregisterAutocompleteCallback()
--!nocheck
-- Voer de volgende code uit in de 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)
-- Om de callback te deregistreren, voer de volgende code uit in de Command Bar
ScriptEditorService:DeregisterAutocompleteCallback("foo")RegisterScriptAnalysisCallback
Teruggaves
()
Codevoorbeelden
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
-- Itereer regel voor regel
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 hier gevonden!",
severity = Enum.Severity.Warning,
})
end
lineNo = lineNo + #newline:gsub("\n+", "\\0%0\\0"):gsub(".%z.", "."):gsub("%z", "")
end
return response
end)UpdateSourceAsync
Parameters
Teruggaves
()
Codevoorbeelden
Update Source Async
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService:UpdateSourceAsync(workspace.Script, function(oldContent)
return oldContent .. " Wereld!"
end)Evenementen
TextDocumentDidChange
ScriptEditorService.TextDocumentDidChange(
Parameters
changesArray:Variant |
Codevoorbeelden
ScriptEditorService.TextDocumentDidChange
--!nocheck
-- Voer de volgende code uit in de Opdrachtbalk
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidChange:Connect(function(scriptDocument, changes)
print("Gewijzigd", scriptDocument, changes)
end)TextDocumentDidClose
Parameters
Codevoorbeelden
ScriptEditorService.TextDocumentDidClose
--!nocheck
-- Voer de volgende code uit in de Opdrachtbalk
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidClose:Connect(function(scriptDocument)
print("Gesloten", scriptDocument)
end)TextDocumentDidOpen
Parameters
Codevoorbeelden
ScriptEditorService.TextDocumentDidOpen
--!nocheck
-- Voer de volgende code uit in de Opdrachtbalk
local ScriptEditorService = game:GetService("ScriptEditorService")
ScriptEditorService.TextDocumentDidOpen:Connect(function(scriptDocument)
print("Geopend", scriptDocument)
end)