Roblox 使用 Luau 程式語言。以下的程式碼範例和表格顯示了 C# 和 Luau 語法之間的一些差異。
行結尾
在 Luau 中不需要分號,但它們不會破壞語法。
保留關鍵字
以下表格將 Luau 的保留關鍵字映射到它們的 C# 等效項。請注意,這不是所有 C# 關鍵字的列表。
| Luau | C# |
|---|---|
| and | |
| break | break |
| do | do |
| if | if |
| else | else |
| elseif | else if |
| then | |
| end | |
| true | true |
| false | false |
| for | for 或 foreach |
| function | |
| in | in |
| local | |
| nil | null |
| not | |
| or | |
| repeat | |
| return | return |
| until | |
| while | while |
註解
Luau中的註解-- 單行註解--[[ 結果輸出:區塊註解--]]
C#中的註解// 單行註解/*區塊註解*/
字串
Luau中的字串-- 多行字串local multiLineString = [[這是一個字串,當打印時,顯示在多行上]]-- 字串連接local s1 = "這是一個字串 "local s2 = "由兩個部分組成。"local endString = s1 .. s2
C#中的字串// 多行字串string multiLineString1 = "這是一個字串,\n當打印時,顯示\n在多行上。";string multiLineString2 = @"這是一個字串,當打印時,顯示在多行上";// 字串連接string s1 = "這是一個字串 ";string s2 = "由兩個部分組成。";string endString = s1 + s2;
表格
要了解更多有關 Luau 中表格的內容,請參見 Tables。
字典表格
您可以在 Luau 中像在 C# 中那樣使用表格作為字典。
Luau中的字典表格local dictionary = {val1 = "這個",val2 = "是"}print(dictionary.val1) -- 輸出 '這個'print(dictionary["val1"]) -- 輸出 '這個'dictionary.val1 = nil -- 從表格中移除 'val1'dictionary["val3"] = "一個字典" -- 覆蓋 'val3' 或設定新的鍵值對
C#中的字典表格Dictionary dictionary = new Dictionary(){{ "val1", "這個" },{ "val2", "是" }};Console.WriteLine(dictionary["val1"]); // 輸出 '這個'dictionary.Remove("val1"); // 從字典中移除 'val1'dictionary["val3"] = "一個字典"; // 覆蓋 'val3' 或設定新的鍵值對dictionary.Add("val3", "一個字典"); // 創建新鍵值對
數字索引表格
您可以像在 C# 中那樣使用表格作為數組。Luau 中的索引從 1 開始,而 C# 中的索引從 0 開始。
Luau中的數字索引表格local npcAttributes = {"強大的", "聰明的"}print(npcAttributes[1]) -- 輸出 '強大的'print(#npcAttributes) -- 輸出列表的大小-- 附加到列表table.insert(npcAttributes, "謙遜的")-- 另一種方法...npcAttributes[#npcAttributes+1] = "謙遜的"-- 在列表的開頭插入table.insert(npcAttributes, 1, "勇敢的")-- 移除給定索引的項目table.remove(npcAttributes, 3)
C#中的數字索引表格List npcAttributes = new List{"強大的", "聰明的"};Console.WriteLine(npcAttributes[0]); // 輸出 '強大的'Console.WriteLine(npcAttributes.Count); // 輸出列表的大小// 附加到列表npcAttributes.Add("謙遜的");// 另一種方法...npcAttributes.Insert(npcAttributes.Count, "謙遜的");// 在列表的開頭插入npcAttributes.Insert(0, "勇敢的");// 移除給定索引的項目npcAttributes.Remove(2);
運算符
條件運算符
| 運算符 | Luau | C# |
|---|---|---|
| 等於 | == | == |
| 大於 | > | > |
| 小於 | < | < |
| 大於或等於 | >= | >= |
| 小於或等於 | <= | <= |
| 不等於 | ~= | != |
| 且 | and | && |
| 或 | or | || |
算術運算符
| Luau | C# | |
|---|---|---|
| 加法 | + | + |
| 減法 | - | - |
| 乘法 | * | * |
| 除法 | / | / |
| 模數 | % | % |
| 指數 | ^ | ** |
變數
在 Luau 中,變數在宣告時不需要指定其類型。Luau 變數沒有訪問修飾符,儘管您可以在 "private" 變數前加上底線以提高可讀性。
Luau中的變數local stringVariable = "值"-- "公共" 宣告local variableName-- "私有" 宣告 - 解析方式相同local _variableName
C#中的變數string stringVariable = "值";// 公共宣告public string variableName// 私有宣告string variableName;
範圍
在 Luau 中,您可以將變數和邏輯寫在比其函數或類更緊湊的範圍內,通過將邏輯嵌套在 do 和 end 關鍵字中,類似於 C# 中的花括號 {}。有關更多詳細資訊,請參見 Scope。
Luau中的範圍local outerVar = '外部範圍文本'do-- 修改 'outerVar'outerVar = '內部範圍修改文本'-- 引入一個局部變數local innerVar = '內部範圍文本'print('1: ' .. outerVar) -- 打印 "1: 內部範圍修改文本"print('2: ' .. innerVar) -- 打印 "2: 內部範圍文本"endprint('3: ' .. outerVar) -- 打印 "3: 內部範圍修改文本"-- 嘗試在這裡打印 'innerVar' 將會失敗
C#中的範圍var outerVar = "外部範圍文本";{// 修改 'outerVar'outerVar = "內部範圍修改文本";// 引入一個局部變數var innerVar = "內部範圍文本";Console.WriteLine("1: " + outerVar); // 打印 "1: 內部範圍修改文本"Console.WriteLine("2: " + innerVar); // 打印 "2: 內部範圍文本"}Console.WriteLine("3: " + outerVar); // 打印 "3: 內部範圍修改文本"// 嘗試在這裡打印 'innerVar' 將會失敗
條件語句
Luau中的條件語句-- 一個條件if boolExpression thendoSomething()end-- 多個條件if not boolExpression thendoSomething()elseif otherBoolExpression thendoSomething()elsedoSomething()end
C#中的條件語句// 一個條件if (boolExpression) {doSomething();}// 多個條件if (!boolExpression) {doSomething();}else if (otherBoolExpression) {doSomething();}else {doSomething();}
條件運算符
Luau中的條件運算符local max = if x > y then x else y
C#中的條件運算符int max = (x > y) ? x : y;
循環
要了解更多有關 Luau 中循環的內容,請參見 Control Structures。
While 和 repeat 循環
Luau中的While和Repeat循環while boolExpression dodoSomething()endrepeatdoSomething()until not boolExpression
C#中的While和Repeat循環while (boolExpression) {doSomething();}do {doSomething();} while (boolExpression)
For 循環
Luau中的通用For循環-- 正向循環for i = 1, 10 dodoSomething()end-- 反向循環for i = 10, 1, -1 dodoSomething()end
C#中的通用For循環// 正向循環for (int i = 1; i <= 10; i++) {doSomething();}// 反向循環for (int i = 10; i >= 1; i--) {doSomething();}
Luau中的表格For循環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
C#中的列表For循環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 中函數的內容,請參見 Functions。
通用函數
Luau中的通用函數
-- 通用函數
local function increment(number)
return number + 1
end
C#中的通用函數
// 通用函數
int increment(int number) {
return number + 1;
}
可變參數數量
Luau中的可變參數數量
-- 可變參數數量
local function variableArguments(...)
print(...)
end
C#中的可變參數數量
// 可變參數數量
void variableArguments(params string[] inventoryItems) {
for (item in inventoryItems) {
Console.WriteLine(item);
}
}
命名參數
Luau中的命名參數
-- 命名參數
local function namedArguments(args)
return args.name .. "的生日: " .. args.dob
end
namedArguments{name="Bob", dob="4/1/2000"}
C#中的命名參數
// 命名參數
string namedArguments(string name, string dob) {
return name + "的生日: " + dob;
}
namedArguments(name: "Bob", dob: "4/1/2000");
嘗試-捕獲結構
Luau中的嘗試-捕獲結構
local function fireWeapon()
if not weaponEquipped then
error("未裝備武器!")
end
-- 繼續...
end
local success, errorMessage = pcall(fireWeapon)
if not success then
print(errorMessage)
end
C#中的嘗試-捕獲結構
void fireWeapon() {
if (!weaponEquipped) {
// 使用用戶定義的異常
throw new InvalidWeaponException("未裝備武器!");
}
// 繼續...
}
try {
fireWeapon();
} catch (InvalidWeaponException ex) {
// 發生了一個錯誤
}