ScriptDocument
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ตัวอย่าง ScriptDocument เป็นตัวแทนของเอกสารของเครื่องแก้ไขสคริปต์สตูดิโอแตกต่างจาก LuaSourceContainer ที่เปิดในตัวแก้ไขโดยที่มันแทนที่สถานะชั่วคราวของเอกสารที่เปิดและการแสดงของมันอยู่ในรูปแบบที่เหมาะสมกับการอ่านและแก้ไขโค้ดมากกว่าการดําเนินการโดยเฉพาะอย่างยิ่ง ScriptDocument สะท้อนการเปลี่ยนแปลงใดๆ ที่ได้ทำกับสคริปต์เปิดในโหมดร่างที่คุณไม่มีคุณสมบัติที่เป็นที่มา
ตัวแก้ไขสคริปต์เองมีอยู่และเปลี่ยนแปลงในกระทู้ที่แตกต่างจากใดๆ DataModel ดังนั้น ScriptDocument จะสร้างสคริปต์เอดิเตอร์ที่เปิดอยู่ แต่ไม่ใช่สคริปต์เอดิเตอร์ที่เปิดเนื่องจากการเลียนแบบจึงมีความล่าช้าเล็กน้อยระหว่างการเปลี่ยนข้อความในเอเดอร์และการอัปเดต ScriptDocumentความล่าช้ามักเกิดขึ้นเพราะ DataModel ไม่ว่าง และมันเล็กมากเกือบตลอดเวลา แต่ก็ยังคงมีอยู่
การมีอยู่ของ ScriptDocument บ่งบอกว่าเอกสารเปิดอยู่ในตัวแก้ไขสคริปต์ทุก ScriptDocument ตัวอย่างมี ScriptEditorService เป็นพ่อของมันแต่ละตัวอย่างปฏิบัติตามมาตรฐานการเข้ารหัสต่อไปนี้:
- ข้อความทั้งหมดใน ScriptDocument ถูกเข้ารหัสเป็น UTF-8
- ตัวบ่งบอกทุกเส้นมี 1 อินดักซ์
- ตัวละครทั้งหมดมีดัชนี 1 และนับไบต์ UTF-8 ไม่ใช่ graphemes ดังนั้นคำเตือนเดียวกันจาก TextBox.CursorPosition จะมีผลบังคับใช้: ตัวละคร Unicode จำนวนมากใช้เวลามากกว่าหนึ่งไบต์
- ทุกช่วงรวมถึงตำแหน่งเริ่มต้นและไม่ได้รวมตำแหน่งสิ้นสุดของพวกเขาดังนั้นการเริ่มต้น == สิ้นสุดหมายถึงช่วงว่าง
ทุก 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
เกิดไฟไหม้เมื่อ ScriptDocument เปลี่ยนแปลงรวมถึงทันทีหลังจากเปลี่ยนแปลงข้อความ
เกิดไฟไหม้เมื่อหมายเลขบรรทัดที่แสดงในตัวแก้ไขเปลี่ยนแปลง
คุณสมบัติ
วิธีการ
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
ส่งค่ากลับ
CursorLine, CursorChar, AnchorLine, 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 นี้:
- สตูดิโอสร้างแถบคําสั่งก่อนที่จะรันปลั๊กอินดังนั้นจึงไม่เสมอไปยิงเหตุการณ์ที่เปิดอยู่แม้ว่าจะปิดและเปิดใหม่ในขณะที่สตูดิโอเปลี่ยนระหว่าง DataModels
- คุณไม่สามารถแก้ไขแถบคําสั่งด้วย 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 ).
ฟังก์ชันโยนข้อผิดพลาดหาก:
- ช่วงไม่ถูกต้อง
- ช่วงจะตัดตัวอักษรยูนิโค้ด ตัวอย่างเช่น เพียงแทนที่บางส่วนของไบต์ของตัวอักษรยูนิโค้ดเท่านั้น
- ตัว newText เองมี UTF-8 ไม่ถูกต้อง
หากฟังก์ชันล้มเหลว จะส่งคืน (false, string)สตริงเป็นคำอธิบายของปัญหาประเภทความล้มเหลวที่พบบ่อยที่สุดคือการไม่ตรงกันระหว่างรุ่นสิ่งนี้เกิดขึ้นเมื่อคุณพยายามโทร EditTextAsync ในช่วงเวลาที่ ScriptDocument ไม่สอดคล้องกับเนื้อหาของเอเดอร์หากเกิดขึ้น คุณสามารถลองแก้ไขใหม่ได้
พารามิเตอร์
ส่งค่ากลับ
ForceSetSelectionAsync
ขอให้เอดิเตอร์ตั้งการเลือกเคอร์เซอร์เป็นมูลค่าอาร์กิวเมนต์ต้องส่งอาร์กิวเมนต์ที่เป็นพานธ์ทั้งสองหรือไม่มีเลยหากไม่มีการส่งผ่าน แต่ละคนจะเริ่มต้นเป็นเหมือนกับอาร์กิวเมนต์เคอร์เซอร์ที่ตรงกันตัวแก้ไขอาจปฏิเสธการอัปเดตเคอร์เซอร์หากเนื้อหาข้อความของเอกสารเปลี่ยนแปลงไม่เหมือนกับ ScriptDocument:RequestSetSelectionAsync() เครื่องแก้ไขจะไม่ปฏิเสธที่จะย้ายเคอร์เซอร์หากเคอร์เซอร์ย้ายตั้งแต่ที่คำขอถูกส่งส่งคืน (true, nil) หากเคอร์เซอร์ได้รับการอัปเดตและ (false, สตริง) พร้อมข้อความอธิบายหากไม่ได้ให้เส้นทางกระทู้ปัจจุบันจนกว่าผู้แก้ไขจะตอบกลับ
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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, สตริง) พร้อมข้อความอธิบายหากไม่ได้ให้เส้นทางกระทู้ปัจจุบันจนกว่าผู้แก้ไขจะตอบกลับ
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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 เปลี่ยนแปลงรวมถึงทันทีหลังจากเปลี่ยนแปลงข้อความ
พารามิเตอร์
ตัวอย่างโค้ด
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)