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# と同じようにテーブルを配列として使用できます。インデックスは 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# |
---|---|---|
等しい To | == | == |
大き於 than | > | > |
少なく於 | < | < |
大き於または等し於より | >= | >= |
小さ于または等し于より少ない | <= | <= |
等しくない: | ~= | != |
そして | 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# のカーリーブレックets で使用されるように、 および キーワード内のロジックをネストして、カーリーブレックのように機能します。詳細は、スコープ を参照してください。
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 の条件付き文で発言
-- 1つの条件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 と repeat ループ
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
}