ScriptDocument
*Bu içerik, yapay zekâ (beta) kullanılarak çevrildi ve hatalar içerebilir. Sayfayı İngilizce görüntülemek için buraya tıkla.
Bir ScriptDocument örneği, bir Studio Kılavuzu Editörü belgesinin bir proxy'sidir.Editörde açılan LuaSourceContainer ile farklıdır, çünkü açık bir belgenin geçici durumunu temsil eder ve temsilinin okuma ve kod düzenleme için daha uygun bir formatta olması daha uygundur.Özellikle, ScriptDocument açık kaynağın özelliklerine sahip olmadığı Taslak Moduna yapılan herhangi bir değişikliği yansıtır, ki bunlar kaynağın özellikleri değildir.
Senaryo Editörü'nün kendisi mevcut ve herhangi bir DataModel 'dan farklı bir iş parçacığında değişiyor, bu yüzden ScriptDocument açık Senaryo Editörü'nü yeniden yazıyor, ancak açık editör değil.Replikasyon nedeniyle, bazen editördeki metni değiştirmek ve güncelleştirmek arasında hafif bir gecikme olur.Gecikme genellikle DataModel meşgul olduğundan kaynaklanır ve neredeyse daima son derece küçüktür, ancak hala var.
Bir ScriptDocument 'nin varlığı, bir belgenin Script Düzenleyicisi'nde açık olduğunu gösterir.Tüm ScriptDocument örneklerinin ebeveyni ScriptEditorService dir.Her bir örnek aşağıdaki kodlama konvansiyonlarına uyuyor:
- ScriptDocument içindeki tüm metin UTF-8 kodlanmıştır.
- Tüm satır indeksleri 1 indeksli.
- Tüm karakter indeksleri 1 indeksli ve UTF-8 baytları sayar, grafemler değil, bu yüzden aynı uyarı TextBox.CursorPosition uygulanır: çok sayıda Unicode karakteri birden fazla bayt alır.
- Tüm menziller başlangıç pozisyonlarını ve son pozisyonlarını içerir ve özel değildir, bu yüzden başlangıç == son boş bir menzil içerir.
ScriptDocument için tüm API'ler Plugin seviye güvenlikte.
Özet
Özellikler
Yöntemler
Belirtilen satırın metnini döndürür. Bir argüman verilmediğinde, mevcut işaretçi konumunun satırını döndürür.
Belgedeki satır sayısını döndürür.
Eğer bir tane varsa temel LuaSourceContainer durumdöndürür, aksi takdirde nil .
Editörde seçilen metni veya seçim yoksa boş bir dizeyi alır.
Script Düzenleyici'nin son bilinen seçimini format olarak döndürür: CursorLine, CursorChar, AnchorLine, AnchorChar . Eğer Script Düzenleyici'nin seçeneği yoksa, CursorLine == AnchorLine ve CursorChar == AnchorChar .
Kurma konumunun ve sabitin en büyüğünü alır. Eğer editörde seçenek yoksa, aynı değerdir.
İşaretçi konumunun ve bağlantının en küçüğünü alır. Eğer editörde seçenek yoksa, aynı değerdir.
- GetText(startLine : number?,startCharacter : number?,endLine : number?,endCharacter : number?):string
Açık editörden metin döndürür.
Düzenleyici değişiklikte şu anda gösterilen satır numaralarını geri döndürür.
Editörün herhangi bir metin seçip seçmediğini döndürür.
ScriptDocument Komut çubuğunu temsil ediyorsa gerçek döndürür.
Editörün bu belge ile ilişkili olduğu istekler kapatılır. Editör, talepyanıt verene kadar mevcut iş parçasını verir.
- EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
Belirtilen aralıktaki metni ( startLine , startColumn ) yeni metin ile değiştirir ( endLine , endColumn ).
- ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
Düzenleyiciye, kuruluş seçimini argüman değerlerine ayarlamasını ister.
- RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
Düzenleyiciye, kuruluş seçimini argüman değerlerine ayarlamasını ister.
Etkinlikler
- SelectionChanged(positionLine : number,positionCharacter : number,anchorLine : number,anchorCharacter : number):RBXScriptSignal
Bir metin değişikliğinden hemen sonra dahil olmak üzere ScriptDocument değiştiğinde ateş eder.
Düzenleyicide gösterilen satır numaraları değiştiğinde ateş eder.
Özellikler
Yöntemler
GetLine
Belirtilen satırın metnini döndürür. Bir argüman verilmediğinde, mevcut işaretçi konumunun satırını döndürür.
Parametreler
Dönüşler
Kod Örnekleri
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
Aktif belgedeki satır sayısını döndürür.
Dönüşler
Kod Örnekleri
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
Eğer bir tane varsa temel LuaSourceContainer durumdöndürür, aksi takdirde nil .
Dönüşler
Kod Örnekleri
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
Editörde seçilen metni veya seçim yoksa boş bir dizeyi alır.
Dönüşler
Kod Örnekleri
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
Script Düzenleyici'nin son bilinen seçimini format olarak döndürür: CursorLine, CursorChar, AnchorLine, AnchorChar . Eğer Script Düzenleyici'nin seçeneği yoksa, CursorLine == AnchorLine ve CursorChar == AnchorChar .
Dönüşler
Kuruluş Çizgisi, İşaretleme Çizgisi, Ankör Çizgisi, Ankör İşaretleme.
GetSelectionEnd
Kurma konumunun ve sabitin en büyüğünü alır. Eğer editörde seçenek yoksa, aynı değerdir.
Dönüşler
Kod Örnekleri
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
İşaretçi konumunun ve bağlantının en küçüğünü alır. Eğer editörde seçenek yoksa, aynı değerdir.
Dönüşler
Kod Örnekleri
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
Açık editörden metin döndürür. 0, 2 veya 4 argümanla çağrılmalıdır:
- 0 argüman ile çağrılırsa, açık editörün tüm içeriğini alır.
- 2 argümanla çağrıldığında, belgenin başlangıcındaki metni alır ( startLine , startColumn ).
- 4 argümanla çağrıldığında, belgenin başlangıcındaki metni alır ( startLine , startColumn ) ve bitişindeki metni alır ( endLine , endColumn ).
Parametreler
Dönüşler
Kod Örnekleri
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
Düzenleyici değişiklikte şu anda gösterilen satır numaralarını geri döndürür.Editör, başlangıç çizgisi ve bitiş çizgisi arasındaki satırları gösterir, dahil.İlk ve son satır yalnızca kısmen görünebilir.Örneğin, son satırın en üst pikeli sadece ekranda olabilir.Ayrıca, kod katlanması başlangıç çizgisi ile bitiş çizgisi arasındaki satırları gizleyebilir.
Dönüşler
Kod Örnekleri
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
Editörün herhangi bir metin seçip seçmediğini döndürür.
Dönüşler
Kod Örnekleri
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
Gerçek döndürürse ScriptDocument Komut çubuğunu temsil ediyorsa. Komut çubuğunun bu API'de özel kuralları ve sınırları vardır:
- Stüdyo, eklentileri çalıştırmadan önce Komut çubuğunu oluşturur, bu yüzden açılan olayı her zaman ateş etmez, ancak DataModels arasında geçiş yaparken Studio'nun kapandığını ve yeniden açıldığını unutmaz.
- Güvenlik nedenleriyle Komut çubuğunu EditTextAsync ile düzenleyemezsiniz.
Dönüşler
Kod Örnekleri
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
Bu belge ile ilişkili olan editörün kapatması gereken istekler.Düzenleyici isteğe yanıt verene kadar mevcut iş parçasını verir.Eğer işlev başarılı olursa, geri döner (true, nil).Eğer işlev başarısız olursa, sorunun açıklaması olarak geri döner (false, dizi).
Bu işlev komut çubuğunu kapatamaz.
Dönüşler
Kod Örnekleri
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
Belirtilen aralıktaki metni ( startLine , startColumn ) ile değiştirir ( endLine , endColumn ) ile newText .Menzil boşsa, işlev metni ( startLine , startColumn ) girer.Metin imlecisi belirtilen aralığın içindeyse, imleç düzenlemenin son konumuna hareket eder.Aksi takdirde, metin imleci hareket etmez.Bu işlev, düzenleyici tarafından düzenlemeyle ilgili bir yanıt alana kadar mevcut iş parçacığını verir.
Eğer işlev başarılı olursa, geri döner ( true , nil ).
Fonksiyon bir hata atar if:
- Menzil geçersizdir.
- Menzil bir Unicode karakterini dilimleyecekti, örneğin Unicode karakterinin yalnızca bazı baytlerini değiştirecekti.
- The newText kendisi geçersiz UTF-8 içerir.
Eğer işlev başarısız olursa, geri döner (false, dizi).Dize, sorunun açıklamasıdır.En yaygın başarısızlık türü bir sürüm uyuşmazlığıdır.Bu, EditTextAsync editörün içeriği ile uyumsuz olduğu sırada ScriptDocument çağırmaya çalıştığınızda meydana gelir.Eğer bu gerçekleşirse, düzenlemeyi yeniden deneyebilirsiniz.
Parametreler
Dönüşler
ForceSetSelectionAsync
Düzenleyiciye, kuruluş seçimini argüman değerlerine ayarlamasını ister.Her iki anahtar argüman geçmeli veya her ikisi de olmalıdır.Her ikisi de geçmezse, her biri aynı kurallara sahip olmak üzere geçerli işaretçi argümanına eşit olur.Dokümanın metin içeriği değiştiğinde editör, okuyucusunu güncellemeyi reddedebilir.Farklı olarak ScriptDocument:RequestSetSelectionAsync() , düzenleyici, istek yapıldığından bu yana kursozu hareket ettirmeyi reddetmeyecektir, eğer kursoz hareket ettiyse.İşaretçi güncellendiyse (true, nil) ve güncellenmediyse (false, dizi) açıklama dizesi ile döndürür.Düzenleyici yanıt verene kadar mevcut iş parçasını verir.
Parametreler
Dönüşler
Kod Örnekleri
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
Düzenleyiciye, kuruluş seçimini argüman değerlerine ayarlamasını ister.Her iki anahtar argüman geçmeli veya her ikisi de olmalıdır.Her ikisi de geçmezse, her biri aynı kurallara sahip olmak üzere geçerli işaretçi argümanına eşit olur.Düzenleyici, belgenin metin içeriği değiştiğinde veya istek yapıldığından bu yana kurpozör hareket ettiğinde kurpozörün güncellenmesini reddedebilir.İşaretçi güncellendiyse (true, nil) ve güncellenmediyse (false, dizi) açıklama dizesi ile döndürür.Düzenleyici yanıt verene kadar mevcut iş parçasını verir.
Parametreler
Dönüşler
Kod Örnekleri
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
Etkinlikler
SelectionChanged
Bir metin değişikliğinden hemen sonra dahil olmak üzere ScriptDocument değiştiğinde ateş eder.
Parametreler
Kod Örnekleri
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
Düzenleyicide gösterilen satır numaraları değiştiğinde ateş eder. Ayrıntılar için ScriptDocument.GetViewport bakın.
Parametreler
Kod Örnekleri
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)