Klasa silnika
ScriptDocument
*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.
Podsumowanie
Metody
MultiEditTextAsync(edits: {any}):Tuple |
Zdarzenia
SelectionChanged(positionLine: number,positionCharacter: number,anchorLine: number,anchorCharacter: number):RBXScriptSignal |
ViewportChanged(startLine: number,endLine: number):RBXScriptSignal |
Materiały referencyjne dotyczące interfejsów API
Metody
CloseAsync
Zwroty
Przykłady kodu
DokumentSkryptu:ZamknijAsync
--!nocheck
-- Uruchom poniższy kod w pasku poleceń
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- Znajdź pierwszy otwarty dokument skryptu
for _, document in documents do
-- Pasek poleceń nie może być zamknięty, więc nie wybieraj go
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`Zamknięto {scriptDocument.Name}`)
else
warn(`Nie udało się zamknąć {scriptDocument.Name} ponieważ: {err}`)
end
else
print("Brak otwartych skryptów")
endEditTextAsync
ForceSetSelectionAsync
Zwroty
Przykłady kodu
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")
endGetLine
Parametry
| Wartość domyślna: "nil" |
Zwroty
Przykłady kodu
ScriptDocument.SelectionChanged i ScriptDocument:GetLine()
--!nocheck
-- Uruchom następujący kod w pasku polecenia, gdy skrypt jest otwarty
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(`Wybrano: Linia {positionLine}, Znak {positionCharacter}`)
print(`Kotwica: Linia {anchorLine}, Znak {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Wybrany tekst linii: {lineText}`)
end)
else
print("Brak otwartych skryptów")
endGetLineCount
Zwroty
Przykłady kodu
ScriptDocument:GetLineCount()
--!nocheck
-- Uruchom następujący kod w Command Bar, gdy skrypt jest otwarty
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(`Skrypt ma {lineCount} linii!`)
else
print("Brak otwartych skryptów")
endGetScript
Zwroty
Przykłady kodu
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")
endGetSelectedText
Zwroty
Przykłady kodu
ScriptDocument:HasSelectedText() i :GetSelectedText()
--!nocheck
-- Uruchom poniższy kod w Command Bar, gdy skrypt jest otwarty
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(`Obecnie wybrany tekst: {selectedText}`)
else
print("Nie ma obecnie wybranego tekstu")
end
end)
else
print("Nie ma otwartych skryptów")
endGetSelectionEnd
Zwroty
Przykłady kodu
ScriptDocument:GetSelectionStart() oraz :GetSelectionEnd()
--!nocheck
-- Uruchom poniższy kod w pasku poleceń, gdy skrypt jest otwarty
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(`Początek wyboru: Linia {startLine}, Znak {startCharacter}`)
print(`Koniec wyboru: Linia {endLine}, Znak {endCharacter}`)
else
print("Nie ma otwartych skryptów")
endGetSelectionStart
Zwroty
Przykłady kodu
ScriptDocument:GetSelectionStart() oraz :GetSelectionEnd()
--!nocheck
-- Uruchom poniższy kod w pasku poleceń, gdy skrypt jest otwarty
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(`Początek wyboru: Linia {startLine}, Znak {startCharacter}`)
print(`Koniec wyboru: Linia {endLine}, Znak {endCharacter}`)
else
print("Nie ma otwartych skryptów")
endGetText
Zwroty
Przykłady kodu
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")
endGetViewport
Zwroty
Przykłady kodu
ScriptDocument:GetViewport
--!nocheck
-- Uruchom poniższy kod w pasku poleceń, gdy skrypt jest otwarty
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(`Aktualnie wyświetlane linie {firstLine} do {lastLine}`)
else
print("Brak otwartych skryptów")
endHasSelectedText
Zwroty
Przykłady kodu
ScriptDocument:HasSelectedText() i :GetSelectedText()
--!nocheck
-- Uruchom poniższy kod w Command Bar, gdy skrypt jest otwarty
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(`Obecnie wybrany tekst: {selectedText}`)
else
print("Nie ma obecnie wybranego tekstu")
end
end)
else
print("Nie ma otwartych skryptów")
endIsCommandBar
Zwroty
Przykłady kodu
ScriptDocument:IsCommandBar()
--!nocheck
-- Uruchom następujący kod w Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if document:IsCommandBar() then
print("Dokument Command Bar:", document)
end
endMultiEditTextAsync
RequestSetSelectionAsync
Zwroty
Przykłady kodu
ScriptDocument:RequestSetSelectionAsync()
--!nocheck
-- Uruchom poniższy kod w Oknie Komend, gdy skrypt jest otwarty
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
-- Pobierz tekst z bieżącej linii kursora
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Wymuś zaznaczenie całej linii tekstu
local success, err = scriptDocument:RequestSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Zaznaczono wybór!")
else
print(`Nie udało się ustawić zaznaczenia, ponieważ: {err}`)
end
else
print("Brak otwartych skryptów")
endZdarzenia
SelectionChanged
ScriptDocument.SelectionChanged(
Przykłady kodu
ScriptDocument.SelectionChanged i ScriptDocument:GetLine()
--!nocheck
-- Uruchom następujący kod w pasku polecenia, gdy skrypt jest otwarty
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(`Wybrano: Linia {positionLine}, Znak {positionCharacter}`)
print(`Kotwica: Linia {anchorLine}, Znak {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Wybrany tekst linii: {lineText}`)
end)
else
print("Brak otwartych skryptów")
endViewportChanged
Przykłady kodu
Łączenie z ScriptDocument.ViewportChanged
--!nocheck
--[[
Aby uruchomić:
1. Upewnij się, że widok Output jest otwarty
2. Uruchom poniższy kod w Command Bar
3. Przewiń w górę i w dół w otwartym oknie Skryptu
Wydruki z zdarzenia ViewportChanged pojawią się w Output
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Utwórz tekst zajmujący wiele linii
local dummyText = string.rep("-- Dummy Text\n", 60)
-- Utwórz skrypt zawierający tekst dummy i otwórz go
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`Nie udało się otworzyć skryptu z powodu: {err}`)
return
end
-- Uzyskaj odniesienie do otwartego skryptu
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`Widok skryptu zmieniony - startLine: {startLine}, endLine: {endLine}`)
end
-- Połącz zdarzenie ViewportChanged z powyższą funkcją, która drukuje pierwszą i ostatnią linię zaktualizowanego widoku
scriptDocument.ViewportChanged:Connect(onViewportChanged)