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) -- 輸出 'this'print(dictionary["val1"]) -- 輸出 'this'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# 相同。索引在 Luau 開始於 1 ,在 C# 開始於 0 。
在 Luau 中數值索引表
local npcAttributes = {"strong", "intelligent"}print(npcAttributes[1]) -- 輸出 'strong'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 中的變量
local stringVariable = "value"-- 「公共」聲明local variableName-- 「私人」聲明 - 以相同方式解析local _variableName
Variables in C#
string stringVariable = "value";// Public declarationpublic string variableName// Private declarationstring variableName;
範圍
在 Luau 中,您可以在更緊湊的範圍內寫變量和邏輯,並將邏輯嵌套在 do 和 end 關鍵字內,類似於 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 和 repeat 循環
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
}