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 中表的更多信息,请参见 表。
字典表
你可以像在 C# 中一样将 Luau 中的表用作字典。
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 中的表用作数组。索引在 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 变量没有访问修饰符,虽然你可以用下划线作为前缀来增加 "私有" 变量的可读性。
Luau 中的变量local stringVariable = "值"-- "公共"声明local variableName-- "私有"声明 - 解析方式相同local _variableName
C# 中的变量string stringVariable = "值";// 公共声明public string variableName// 私有声明string variableName;
范围
在 Luau 中,你可以通过将逻辑嵌套在 do 和 end 关键字中来写出比函数或类更紧凑的变量和逻辑,类似于 C# 中的花括号 {}。有关详细信息,请参见 范围。
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 中循环的更多信息,请参见 控制结构。
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 中函数的更多信息,请参见 函数。
通用函数
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 .. "'s birthday: " .. args.dob
end
namedArguments{name="Bob", dob="4/1/2000"}
C# 中的命名参数
// 命名参数
string namedArguments(string name, string dob) {
return name + "'s birthday: " + dob;
}
namedArguments(name: "Bob", dob: "4/1/2000");
Try-catch 结构
Luau 中的 Try/Catch 结构
local function fireWeapon()
if not weaponEquipped then
error("没有装备武器!")
end
-- 继续...
end
local success, errorMessage = pcall(fireWeapon)
if not success then
print(errorMessage)
end
C# 中的 Try/Catch 结构
void fireWeapon() {
if (!weaponEquipped) {
// 使用用户定义的异常
throw new InvalidWeaponException("没有装备武器!");
}
// 继续...
}
try {
fireWeapon();
} catch (InvalidWeaponException ex) {
// 发生了错误
}