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 中使用表作为 array,与 C# 中一样。索引在 Luau 中开始于 1 ,在 C# 中开始于 0 。
在 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 中,您可以在更紧密的范围内写变量和逻辑,比其函数或类,通过在 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 中循环的更多信息,请参阅 控制结构。
while 和重复循环
在 Luau 中使用 while 和重复循环
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
}