Roblox ใช้ภาษาโปรแกรม Luau ตัวอย่างโค้ดและตารางต่อไปนี้แสดงให้เห็นถึงความแตกต่างบางอย่างระหว่างภาษาสัญลักษณ์สำหรับ C# และ Luau
จุดจบบรรทัด
คุณไม่ต้องใช้เครื่องหมายจุลภาคใน Luau แต่พวกเขาไม่ทำลายสัญลักษณ์ของภาษา
คําสําคัญสํารอง
ตารางต่อไปนี้มีคีย์เวิร์ดสำรองของ Luau ถูกแปลงเป็นคีย์เวิร์ดเทียบเท่าของ C# โปรดทราบว่าไม่แสดงคีย์เวิร์ด C# ทั้งหมด
ลูอา | C# |
---|---|
and | |
break | break |
do | do |
if | if |
else | else |
elseif | else if |
then | |
end | |
true | true |
false | false |
for | for or foreach |
function | |
in | in |
local | |
nil | null |
not | |
or | |
repeat | |
return | return |
until | |
while | while |
คําติชม
ความคิดเห็นใน Luau
-- ความคิดเห็นเดียวบรรทัด--[[ ผลผลิตที่ได้:Block comment--]]
Comments in C#
// Single line comment/*Block comment*/
สตริง
สตริงใน Luau
-- สตริงหลายบรรทัดlocal multiLineString = [[This is a string that,when printed, appearson multiple lines]]-- การรวมกันlocal s1 = "This is a string "local s2 = "made with two parts."local endString = s1 .. s2
Strings in C#
// Multi-line stringstring multiLineString1 = "This is a string that,\nwhen printed, appears\n on multiple lines.";string multiLineString2 = @"This is a string that,when printed, appearson multiple lines";// Concatenationstring s1 = "This is a string ";string s2 = "made with two parts.";string endString = s1 + s2;
ตาราง
เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับตารางใน Luau ดู ตาราง
ตารางสารานุกรม
คุณสามารถใช้ตารางใน Luau เป็นสารานุกรมเหมือนใน C#
ตารางสารานุกรมใน Luau
local dictionary = {val1 = "this",val2 = "is"}print(dictionary.val1) -- ผลผลิต 'นี้'print(dictionary["val1"]) -- ผลผลิต 'นี้'dictionary.val1 = nil -- ลบ 'val1' ออกจากตารางdictionary["val3"] = "a dictionary" -- Overwrites 'val3' or sets new key-value pair
Dictionary Tables in C#
Dictionary dictionary = new Dictionary(){{ "val1", "this" },{ "val2", "is" }};Console.WriteLine(dictionary["val1"]); // Outputs 'this'dictionary.Remove("val1"); // Removes 'val1' from dictionarydictionary["val3"] = "a dictionary"; // Overwrites 'val3' or sets new key-value pairdictionary.Add("val3", "a dictionary"); // Creates a new key-value pair
ตารางที่จัดเรียงตามตัวเลข
คุณสามารถใช้ตารางใน Luau เป็นเวกเตอร์เหมือนใน C# ดัชนีเริ่มต้นที่ 1 ใน Luau และ 0 ใน C#
ตารางที่จัดเรียงตามตัวเลขใน Luau
local npcAttributes = {"strong", "intelligent"}print(npcAttributes[1]) -- ผลผลิต 'แข็งแกร่ง'print(#npcAttributes) -- แสดงขนาดของรายการ-- เพิ่มลงในรายการtable.insert(npcAttributes, "humble")-- อีกวิธีหนึ่ง...npcAttributes[#npcAttributes+1] = "humble"-- เพิ่มในตอนต้นของรายการtable.insert(npcAttributes, 1, "brave")-- ลบรายการที่ดัชนีที่กำหนดtable.remove(npcAttributes, 3)
Numerically-Indexed Tables in C#
List npcAttributes = new List{"strong", "intelligent"};Console.WriteLine(npcAttributes[0]); // Outputs 'strong'Console.WriteLine(npcAttributes.Count); // Outputs the size of the list// Append to the listnpcAttributes.Add("humble");// Another way...npcAttributes.Insert(npcAttributes.Count, "humble");// Insert at the beginning of the listnpcAttributes.Insert(0, "brave");// Remove item at a given indexnpcAttributes.Remove(2);
ผู้ปฏิบัติการ
ตัวประกอบเงื่อนไข
ผู้ปฏิบัติการ | ลูอา | C# |
---|---|---|
เท่ากับ | == | == |
มากกว่า | > | > |
น้อยกว่า | < | < |
มากกว่าหรือเท่ากับ | >= | >= |
น้อยกว่าหรือเท่ากับ | <= | <= |
ไม่เท่ากับ | ~= | != |
และ | and | && |
Or | or | || |
ตัวประกอบทางคณิตศาสตร์
Luau | C# | |
---|---|---|
เพิ่ม | + | + |
การหักลบ | - | - |
การคูณ | * | * |
ดิวิชัน | / | / |
โมดูล | % | % |
การคูณอัตราเร่ง | ^ | ** |
ตัวแปร
ใน Luau ตัวแปรไม่ระบุชนิดของพวกเขาเมื่อคุณประกาศพวกเขาตัวแปร Luau ไม่มีตัวแก้ไขการเข้าถึงแม้ว่าคุณอาจใส่คํานําหน้า "ส่วนตัว" กับเครื่องหมายขีดข่วนเพื่อให้อ่านได้ง่าย
ตัวแปรใน Luau
local stringVariable = "value"-- คําประกาศ "สาธารณะ"local variableName-- การประกาศ "ส่วนตัว" - แยกวิเคราะห์เช่นเดียวกันlocal _variableName
Variables in C#
string stringVariable = "value";// Public declarationpublic string variableName// Private declarationstring variableName;
ขอบเขต
ใน Luau คุณสามารถเขียนตัวแปรและโลจิสติกในขอบเขตที่แคบกว่าฟังก์ชันหรือคลาสของพวกเขาโดยนิ่งโลจิสติกภายใน และ คีย์เวิร์ดที่คล้ายกับเครื่องหมายเคลือบเกลียวใน C#สำหรับรายละเอียดเพิ่มเติม ดู ขอบเขต
สํารวจใน Luau
local outerVar = 'Outer scope text'do-- แก้ไข 'outerVar'outerVar = 'Inner scope modified text'-- แนะนำตัวแปรท้องถิ่นlocal innerVar = 'Inner scope text'print('1: ' .. outerVar) -- พิมพ์ "1: ข้อความที่แก้ไขขอบเขตภายใน"print('2: ' .. innerVar) -- พิมพ์ "2: ข้อความขอบภายใน"endprint('3: ' .. outerVar) -- พิมพ์ "3: "ข้อความที่แก้ไขขอบเขตภายใน"-- Attempting to print 'innerVar' here would fail
Scoping in C#
var outerVar = "Outer scope text";{// Modify 'outerVar'outerVar = "Inner scope modified text";// Introduce a local variablevar innerVar = "Inner scope text";Console.WriteLine("1: " + outerVar); // prints "1: Inner scope modified text"Console.WriteLine("2: " + innerVar); // prints "2: Inner scope text"}Console.WriteLine("3: " + outerVar); // prints "3: "Inner scope modified text"// Attempting to print 'innerVar' here would fail
คําสั่งเงื่อนไข
คําสั่งเงื่อนไขใน Luau
-- เงื่อนไขหนึ่งif boolExpression thendoSomething()end-- เงื่อนไขหลายอย่างif not boolExpression thendoSomething()elseif otherBoolExpression thendoSomething()elsedoSomething()end
Conditional Statements in C#
// One conditionif (boolExpression) {doSomething();}// Multiple conditionsif (!boolExpression) {doSomething();}else if (otherBoolExpression) {doSomething();}else {doSomething();}
ตัวประกอบเงื่อนไข
ตัวประกอบเงื่อนไขใน Luau
local max = if x > y then x else y
Conditional Operator in C#
int max = (x > y) ? x : y;
ลูป
เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับลูปใน Luau ดู โครงสร้างการควบคุม
ในขณะที่และทำซ้ำลูป
ในขณะที่และทำซ้ำลูปใน Luau
while boolExpression dodoSomething()endrepeatdoSomething()until not boolExpression
While and Repeat Loops in C#
while (boolExpression) {doSomething();}do {doSomething();} while (boolExpression)
สำหรับลูป
ทั่วไปสำหรับลูปใน Luau
-- ลูปข้ามไปข้างหน้าfor i = 1, 10 dodoSomething()end-- ลูปย้อนกลับfor i = 10, 1, -1 dodoSomething()end
Generic For Loops in C#
// Forward loopfor (int i = 1; i <= 10; i++) {doSomething();}// Reverse loopfor (int i = 10; i >= 1; i--) {doSomething();}
สำหรับลูปเหนือตารางใน Luau
local abcList = {"a", "b", "c"}for i, v in ipairs(abcList) doprint(v)endlocal abcDictionary = { a=1, b=2, c=3 }for k, v in pairs(abcDictionary) doprint(k, v)end
For Loops Over Lists in C#
List<string> abcList = new List<string>{"a", "b", "c"};foreach (string v in abcList) {Console.WriteLine(v);}Dictionary<string, int> abcDictionary = new Dictionary<string, int>{ {"a", 1}, {"b", 2}, {"c", 3} };foreach (KeyValuePair<string, int> entry in abcDictionary) {Console.WriteLine(entry.Key + " " + entry.Value);}
Luau ยังสนับสนุน การซ้ำแบบทั่วไป ซึ่งทำให้การทำงานกับตารางง่ายขึ้นอีก
ฟังก์ชัน
เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับฟังก์ชันใน Luau ดู ฟังก์ชัน
ฟังก์ชันทั่วไป
ฟังก์ชันทั่วไปใน Luau
-- ฟังก์ชันทั่วไป
local function increment(number)
return number + 1
end
Generic Functions in C#
// Generic function
int increment(int number) {
return number + 1;
}
ตัวเลขอาร์กิวเมนต์แปรผัน
หมายเลขอาร์กิวเมนต์แปรใน Luau
-- ตัวเลขอาร์กิวเมนต์แปรผัน
local function variableArguments(...)
print(...)
end
Variable Argument Number in C#
// Variable argument number
void variableArguments(params string[] inventoryItems) {
for (item in inventoryItems) {
Console.WriteLine(item);
}
}
อาร์กิวเมนต์ที่ชื่อว่า
ตัวอ้างอิงที่ชื่อใน Luau
-- อาร์กิวเมนต์ที่ชื่อว่า
local function namedArguments(args)
return args.name .. "'s birthday: " .. args.dob
end
namedArguments{name="Bob", dob="4/1/2000"}
Named Arguments in C#
// Named arguments
string namedArguments(string name, string dob) {
return name + "'s birthday: " + dob;
}
namedArguments(name: "Bob", dob: "4/1/2000");
โครงสร้างจับคู่ลอง
ลอง/จับโครงสร้างใน Luau
local function fireWeapon()
if not weaponEquipped then
error("No weapon equipped!")
end
-- ดำเนินการต่อ...
end
local success, errorMessage = pcall(fireWeapon)
if not success then
print(errorMessage)
end
Try/Catch Structures in C#
void fireWeapon() {
if (!weaponEquipped) {
// Use a user-defined exception
throw new InvalidWeaponException("No weapon equipped!");
}
// Proceed...
}
try {
fireWeapon();
} catch (InvalidWeaponException ex) {
// An error was raised
}