A ScriptDocument 인스턴스는 Studio 스크립트 편집기의 문서의 프록시입니다.편집기에서 열린 문서의 임시 상태를 나타내고 해당 표현이 코드를 읽고 편집하기에 더 적합한 형식으로 되어 있기 때문에 편집기에서 열린 문서와는 다릅니다 LuaSourceContainer 그리고 해당 표현은 실행하는 것보다 코드를 읽고 편집하기에 더 적합한 형식입니다.특히, ScriptDocument는 소스 속성이 없는 초안 모드에서 열린 스크립트에 적용된 모든 변경 사항을 반영합니다.
스크립트 편집기 자체가 존재하고 다른 스레드에서 변경되므로 열린 스크립트 편집기를 복제하지만 열린 편집기가 아닙니다.복제로 인해 편집기에서 텍스트를 변경하고 ScriptDocument지연은 일반적으로 DataModel 가 바빠서 발생하며, 거의 항상 매우 작지만 여전히 존재합니다.
ScriptDocument의 존재는 문서가 스크립트 편집기에서 열려 있음을 나타냅니다.모든 ScriptDocument 인스턴스에는 ScriptEditorService 가 부모로 있습니다.각 인스턴스는 다음 인코딩 규칙을 준수합니다:
- ScriptDocument의 모든 텍스트가 UTF-8로 인코딩되었습니다.
- 모든 라인 인덱스가 1-인덱스입니다.
- 모든 문자 인덱스는 1-인덱스이며 UTF-8 바이트를 계산하고 그래픽 요소가 아니므로 TextBox.CursorPosition의 동일한 경고가 적용됩니다: 많은 유니코드 문자는 하나 이상의 바이트를 사용합니다.
- 모든 범위는 시작 위치와 종료 위치를 포함하며 종료 == 시작은 빈 범위를 의미합니다. All ranges are inclusive of their start position and exclusive of their end position, so start == end implies an empty range.
모든 API는 ScriptDocument 에서 플러그인 수준의 보안에 있습니다.
요약
메서드
지정된 줄의 텍스트를 반환합니다. 인수가 제공되지 않으면 현재 커서 위치의 줄을 반환합니다.
문서의 줄 수를 반환합니다.
기본 LuaSourceContainer 인스턴스를 반환하며, 존재하는 경우 그렇지 않으면 nil .
편집기에서 선택한 텍스트 또는 선택 사항이 없으면 빈 문자열을 가져옵니다.
스크립트 편집기의 마지막으로 알려진 선택을 다음 형식으로 반환합니다: CursorLine, CursorChar, AnchorLine, AnchorChar . 스크립트 편집기에 선택이 없는 경우, CursorLine == AnchorLine 및 CursorChar == AnchorChar .
커서 위치와 앵커 중 더 큰 값을 가져옵니다. 편집기에 선택이 없으면 동일한 값입니다.
커서 위치와 앵커 중 가장 작은 값을 가져옵니다. 편집기에 선택이 없으면 동일한 값입니다.
- GetText(startLine : number?,startCharacter : number?,endLine : number?,endCharacter : number?):string
열린 편집기에서 텍스트를 반환합니다.
편집기 변경에서 현재 표시된 줄 번호를 반환합니다.
편집기에 텍스트가 선택되었는지 여부를 반환합니다.
ScriptDocument가 명령 모음을 나타내는 경우 진실을 반환합니다.
이 문서와 연결된 편집기가 닫는 요청. 편집기가 요청에 응답할 때까지 현재 스레드를 제공합니다.
- EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
지정된 범위에 있는 텍스트( startLine , startColumn )를 새로운 텍스트로 바꿉니다( endLine , endColumn ).
- ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
편집기에 커서 선택을 인수 값으로 설정하도록 요청합니다.
- RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
편집기에 커서 선택을 인수 값으로 설정하도록 요청합니다.
이벤트
- SelectionChanged(positionLine : number,positionCharacter : number,anchorLine : number,anchorCharacter : number):RBXScriptSignal
텍스트 변경 즉시 포함하여 스크립트 문서가 변경될 때 발생합니다.
편집기에서 표시되는 라인 번호가 변경될 때 발생합니다.
속성
메서드
GetLine
지정된 줄의 텍스트를 반환합니다. 인수가 제공되지 않으면 현재 커서 위치의 줄을 반환합니다.
매개 변수
반환
코드 샘플
ScriptDocument.SelectionChanged and ScriptDocument:GetLine()
--!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
활성 문서의 줄 수를 반환합니다.
반환
코드 샘플
ScriptDocument:GetLineCount()
--!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
기본 LuaSourceContainer 인스턴스를 반환하며, 존재하는 경우 그렇지 않으면 nil .
반환
코드 샘플
ScriptDocument:GetScript()
--!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
편집기에서 선택한 텍스트 또는 선택 사항이 없으면 빈 문자열을 가져옵니다.
반환
코드 샘플
ScriptDocument:HasSelectedText() and :GetSelectedText()
--!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
GetSelection
스크립트 편집기의 마지막으로 알려진 선택을 다음 형식으로 반환합니다: CursorLine, CursorChar, AnchorLine, AnchorChar . 스크립트 편집기에 선택이 없는 경우, CursorLine == AnchorLine 및 CursorChar == AnchorChar .
반환
커서라인, 커서캐릭터, 앵커라인, 앵커캐릭터.
GetSelectionEnd
커서 위치와 앵커 중 더 큰 값을 가져옵니다. 편집기에 선택이 없으면 동일한 값입니다.
반환
코드 샘플
ScriptDocument:GetSelectionStart() and :GetSelectionEnd()
--!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
커서 위치와 앵커 중 가장 작은 값을 가져옵니다. 편집기에 선택이 없으면 동일한 값입니다.
반환
코드 샘플
ScriptDocument:GetSelectionStart() and :GetSelectionEnd()
--!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
열린 편집기에서 텍스트를 반환합니다. 0, 2 또는 4개의 인수로 호출해야 합니다:
- 0개의 인수로 호출하면 열린 편집기의 전체 콘텐츠를 가져옵니다.
- 2개의 인수로 호출하면 문서의 텍스트가 ( startLine , startColumn )에서 시작하여 가져옵니다.
- 4개의 인수로 호출하면 문서의 텍스트가 ( startLine , startColumn )에서 시작하여 ( endLine , endColumn )에 끝나도록 가져옵니다.
매개 변수
반환
코드 샘플
ScriptDocument:GetText()
--!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
편집기 변경에서 현재 표시된 줄 번호를 반환합니다.편집기는 startLine과 endLine 사이의 줄을 포함하여 표시합니다.첫 번째와 마지막 줄은 부분적으로만 표시될 수 있습니다.예를 들어, 마지막 줄의 상위 픽셀만 화면에 표시될 수 있습니다.또한 코드 폴딩은 startLine과 endLine 사이의 줄을 숨길 수 있습니다.
반환
코드 샘플
ScriptDocument:GetViewport
--!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
편집기에 텍스트가 선택되었는지 여부를 반환합니다.
반환
코드 샘플
ScriptDocument:HasSelectedText() and :GetSelectedText()
--!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
ScriptDocument 가 명령 모음을 나타낸다면 진실을 반환합니다. 명령 모음에는 이 API에서 특별한 규칙과 제한이 있습니다.
- 스튜디오는 플러그인을 실행하기 전에 명령 모음을 생성하므로 항상 열린 이벤트를 발사하지는 않지만 데이터 모델 간에 스튜디오가 전환되는 동안 닫고 다시 열립니다.
- 보안상의 이유로 명령 모음을 EditTextAsync로 편집할 수 없습니다.
반환
코드 샘플
ScriptDocument:IsCommandBar()
--!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
이 문서와 연결된 편집기가 닫는 요청.편집기가 요청에 응답할 때까지 현재 스레드를 렌더링합니다.함수가 성공하면 (true, nil)을 반환합니다.함수가 실패하면 오류 설명으로 (false, 문자열)를 반환합니다.
이 함수는 명령 모음을 닫을 수 없습니다.
반환
코드 샘플
ScriptDocument:CloseAsync
--!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
지정된 범위에 있는 텍스트를 ( startLine , startColumn )에서 ( endLine , endColumn )로 바꾸고, newText 로 바꿉니다.범위가 비어 있으면 함수는 텍스트를 ( startLine , startColumn )에 삽입합니다.텍스트 커서가 지정된 범위 내에 있으면 커서가 편집의 끝 위치로 이동합니다.그렇지 않으면 텍스트 커서가 이동하지 않습니다.이 함수는 편집에 대한 응답을 받을 때까지 현재 스레드를 반환합니다.
함수가 성공하면 (true, nil )를 반환합니다.
함수는 다음 경우 오류를 발생시킵니다:
- 범위가 유효하지 않은.
- 범위는 유니코드 문자를 잘라서 예를 들어 유니코드 문자의 일부 바이트만 바꿉니다.
- The newText 자체에는 유효하지 않은 UTF-8이 포함되어 있습니다.
함수가 실패하면 (false, string)을 반환합니다.문자열은 문제의 설명입니다.가장 일반적인 오류 유형은 버전 불일치입니다.이는 편집기 콘텐츠와 동기화되지 않는 동안 를 호출하려고 할 때 발생합니다.이런 경우 편집을 다시 시도할 수 있습니다.
매개 변수
반환
ForceSetSelectionAsync
편집기에 커서 선택을 인수 값으로 설정하도록 요청합니다.두 개의 앵커 인수를 모두 전달해야 하거나 아무것도 전달하지 않아야 합니다.둘 중 어느 것도 전달되지 않으면 각각 해당 커서 인수와 동일하게 기본값으로 설정됩니다.문서의 텍스트 콘텐츠가 변경되면 편집기가 커서를 업데이트하지 않을 수 있습니다.ScriptDocument:RequestSetSelectionAsync() 와 달리 편집기는 요청이 이루어진 이후 커서가 이동되었으면 커서를 이동하지 않습니다.커서가 업데이트되었는지 여부에 따라 반환(true, nil)하고 그렇지 않은 경우 설명 문자열을 사용합니다(false, string).편집기가 응답할 때까지 현재 스레드를 제공합니다.
매개 변수
반환
코드 샘플
ScriptDocument:ForceSetSelectionAsync()
--!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
편집기에 커서 선택을 인수 값으로 설정하도록 요청합니다.두 개의 앵커 인수를 모두 전달해야 하거나 아무것도 전달하지 않아야 합니다.둘 중 어느 것도 전달되지 않으면 각각 해당 커서 인수와 동일하게 기본값으로 설정됩니다.문서의 텍스트 콘텐츠가 변경되었거나 요청 후 커서가 이동되었으면 편집기가 커서를 업데이트하지 않을 수 있습니다.커서가 업데이트되었는지 여부에 따라 반환(true, nil)하고 그렇지 않은 경우 설명 문자열을 사용합니다(false, string).편집기가 응답할 때까지 현재 스레드를 제공합니다.
매개 변수
반환
코드 샘플
ScriptDocument:RequestSetSelectionAsync()
--!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
이벤트
SelectionChanged
텍스트 변경 즉시 포함하여 스크립트 문서가 변경될 때 발생합니다.
매개 변수
코드 샘플
ScriptDocument.SelectionChanged and ScriptDocument:GetLine()
--!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
편집기에서 표시되는 라인 번호가 변경될 때 발생합니다. 자세한 내용은 ScriptDocument.GetViewport에 참조하십시오.
매개 변수
코드 샘플
Demonstrates using ScriptDocument.ViewportChanged to print the start and end line of the script's viewport when it changes.
To run:
- Ensure Output view is open
- Run the below code in the Command Bar
- Scroll up and down in the opened Script window
--!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)