ScriptDocument
*Dieser Inhalt wurde mit KI (Beta) übersetzt und kann Fehler enthalten. Um diese Seite auf Englisch zu sehen, klicke hier.
Eine ScriptDocument ist ein Proxy des Dokuments eines Studio-Skript-Editors.Es unterscheidet sich vom LuaSourceContainer , das im Editor geöffnet wird, da es den ephemeren Zustand eines offenen Dokuments darstellt, und seine Darstellung in einem Format ist, das besser geeignet ist, um Code zu lesen und zu bearbeiten, als ihn auszuführen.Insbesondere reflektiert ScriptDocument alle Änderungen, die am offenen Skript im Entwurfsmodus vorgenommen wurden, die die Eigenschaft der Quelle nicht hat.
Der Skript-Editor selbst existiert und ändert sich auf einem anderen Thread als jede DataModel, so repliziert der ScriptDocument den geöffneten Skript-Editor, aber er ist nicht der geöffnete Editor.Aufgrund der Replikation gibt es manchmal eine leichte Verzögerung zwischen dem Ändern des Textes im Editor und der Aktualisierung des ScriptDocument.Die Verzögerung tritt in der Regel auf, weil das DataModel beschäftigt ist und es fast immer extrem klein ist, aber es existiert immer noch.
Die Existenz eines ScriptDocument zeigt an, dass ein Dokument im Skript-Editor geöffnet ist.Alle ScriptDocument Instanzen haben ScriptEditorService als ihren übergeordnetes Teil.Jede Instanz hält sich an die folgenden Codierungsconventionen:
- Alle Text in ScriptDocument ist UTF-8 codiert.
- Alle Zeilenindizes sind 1-indexiert.
- Alle Zeichenindizes sind 1-indexiert und zählen UTF-8-Bytes, nicht Grapheme, da die gleiche Warnung von TextBox.CursorPosition gilt: Viele Unicode-Zeichen nehmen mehr als einen Byte ein.
- Alle Bereiche enthalten ihre Startposition und sind exklusiv für ihre Endposition, sodass start == end eine leere Reichweite impliziert.
Alle APIs für ScriptDocument sind auf Plugin Sicherheitsniveau.
Zusammenfassung
Methoden
Gibt den Text der angegebenen Zeile zurück. Wenn kein Argument angegeben wird, gibt er die Zeile der aktuellen Cursorposition zurück.
Gibt die Anzahl der Zeilen im Dokument zurück.
Gibt die zugrunde liegende LuaSourceContainer Instanz zurück, wenn es eine gibt, sonst nil .
Holen Sie sich den Text aus, der im Editor ausgewählt wurde, oder eine leere Zeichenkette, wenn keine Auswahl vorliegt.
Gibt die letzte bekannte Auswahl des Skripteditors im Format zurück: CursorLine, CursorChar, AnchorLine, AnchorChar . Wenn der Skripteditor keine Auswahl hat, CursorLine == AnchorLine und CursorChar == AnchorChar .
Holt die größere Position des Cursors und des Ankers. Wenn der Editor keine Auswahl hat, sind sie dasselbe Wert.
Holt die kleinere Position des Cursors und des Ankers. Wenn der Editor keine Auswahl hat, sind sie dasselbe Wert.
- GetText(startLine : number?,startCharacter : number?,endLine : number?,endCharacter : number?):string
Gibt Text aus dem geöffneten Editor zurück.
Gibt die derzeit angezeigten Zeilennummern im Editor-Änderungsprozess zurück.
Gibt zurück, ob der Editor irgendeinen Text ausgewählt hat oder nicht.
Gibt wahr zurück, wenn die ScriptDocument die Befehlsleiste darstellt.
Anfragen, die der Editor mit diesem Dokument verknüpft, schließen. Gibt den aktuellen Thread frei, bis der Editor auf die Anfrage antwortet.
- EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
Ersetzt den Text in der angegebenen Reichweite von ( startLine, startColumn ) bis ( endLine, endColumn ) durch neuen Text.
- ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
Bittet den Editor, seine Cursorauswahl auf die Argumentwerte zu setzen.
- RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
Bittet den Editor, seine Cursorauswahl auf die Argumentwerte zu setzen.
Events
- SelectionChanged(positionLine : number,positionCharacter : number,anchorLine : number,anchorCharacter : number):RBXScriptSignal
Feuert, wenn sich das Skriptdokument ändert, einschließlich unmittelbar nach einer Textänderung.
Feuert, wenn sich die angezeigten Zeilennummern im Editor ändern.
Eigenschaften
Methoden
GetLine
Parameter
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Selected line text: {lineText}`)
end)
else
print("No scripts open")
end
GetLineCount
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local lineCount = scriptDocument:GetLineCount()
print(`The script has {lineCount} lines!`)
else
print("No scripts open")
end
GetScript
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local openScript = scriptDocument:GetScript()
print(`Currently open script: {openScript:GetFullName()}`)
else
print("No scripts open")
end
GetSelectedText
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`Currently selected text: {selectedText}`)
else
print("No text currently selected")
end
end)
else
print("No scripts open")
end
GetSelectionEnd
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`Selection start: Line {startLine}, Char {startCharacter}`)
print(`Selection end: Line {endLine}, Char {endCharacter}`)
else
print("No scripts open")
end
GetSelectionStart
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`Selection start: Line {startLine}, Char {startCharacter}`)
print(`Selection end: Line {endLine}, Char {endCharacter}`)
else
print("No scripts open")
end
GetText
Parameter
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local text = scriptDocument:GetText()
print(`Script contents: {text}`)
else
print("No scripts open")
end
GetViewport
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local firstLine, lastLine = scriptDocument:GetViewport()
print(`Currently viewing lines {firstLine} to {lastLine}`)
else
print("No scripts open")
end
HasSelectedText
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`Currently selected text: {selectedText}`)
else
print("No text currently selected")
end
end)
else
print("No scripts open")
end
IsCommandBar
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if document:IsCommandBar() then
print("Command bar document:", document)
end
end
CloseAsync
Rückgaben
Code-Beispiele
--!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
EditTextAsync
Parameter
Rückgaben
ForceSetSelectionAsync
Parameter
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- Get the text on the cursor's current line
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Force select the entire line of text
local success, err = scriptDocument:ForceSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Set selection!")
else
print(`Failed to set selection because: {err}`)
end
else
print("No scripts open")
end
RequestSetSelectionAsync
Parameter
Rückgaben
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- Get the text on the cursor's current line
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Force select the entire line of text
local success, err = scriptDocument:RequestSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Set selection!")
else
print(`Failed to set selection because: {err}`)
end
else
print("No scripts open")
end
Events
SelectionChanged
Parameter
Code-Beispiele
--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Selected line text: {lineText}`)
end)
else
print("No scripts open")
end
ViewportChanged
Parameter
Code-Beispiele
--!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)