Luau と C# の比較

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

Roblox は Luau プログラミング言語を使用しています。次のコードサンプルとテーブルは、C# と Luau の構文の違いの一部を示しています。

行の終わり

Luau ではセミコロンは必要ありませんが、構文を破壊しません。

予約キーワード

次の表には、Luau の予約キーが C# の同等にマップされています。すべての C# キーが表示されているわけではありません。

ルアウC#
and
breakbreak
dodo
ifif
elseelse
elseifelse if
then
end
truetrue
falsefalse
forfor or foreach
function
inin
local
nilnull
not
or
repeat
returnreturn
until
whilewhile

コメント

Luau のコメント

-- 単行コメントするメント
--[[ 結果の出力:
Block comment
--]]
Comments in C#

// Single line comment
/*
Block comment
*/

ストリング

Luau の弦

-- マルチラインの文字列
local multiLineString = [[This is a string that,
when printed, appears
on multiple lines]]
-- 結合
local s1 = "This is a string "
local s2 = "made with two parts."
local endString = s1 .. s2
Strings in C#

// Multi-line string
string multiLineString1 = "This is a string that,\nwhen printed, appears\n on multiple lines.";
string multiLineString2 = @"This is a string that,
when printed, appears
on multiple lines";
// Concatenation
string 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 dictionary
dictionary["val3"] = "a dictionary"; // Overwrites 'val3' or sets new key-value pair
dictionary.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 list
npcAttributes.Add("humble");
// Another way...
npcAttributes.Insert(npcAttributes.Count, "humble");
// Insert at the beginning of the list
npcAttributes.Insert(0, "brave");
// Remove item at a given index
npcAttributes.Remove(2);

オペレータ

条件付き演算子

オペレータルアウC#
等しい To====
大き於 than>>
少なく於<<
大き於または等し於より>=>=
小さ于または等し于より少ない<=<=
等しくない:~=!=
そしてand&&
Oror||

算術演算子

LuauC#
追加++
減算--
乗算**
分割//
モジュール%%
経験乗数^**

変数

Luau では、変数は宣言するときにそのタイプを指定しません。Luau 変数にはアクセス修正子はありませんが、読みやすさのために「プライベート」変数にアンダースプライベートサーバーアを付けることができます。

Luau の変数

local stringVariable = "value"
-- 「公開」宣言
local variableName
-- 「プライベート」宣言 - 同じ方法で解析
local _variableName
Variables in C#

string stringVariable = "value";
// Public declaration
public string variableName
// Private declaration
string 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: 内部スコープテキスト」
end
print('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 variable
var 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 then
doSomething()
end
-- 複数の条件
if not boolExpression then
doSomething()
elseif otherBoolExpression then
doSomething()
else
doSomething()
end
Conditional Statements in C#

// One condition
if (boolExpression) {
doSomething();
}
// Multiple conditions
if (!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 do
doSomething()
end
repeat
doSomething()
until not boolExpression
While and Repeat Loops in C#

while (boolExpression) {
doSomething();
}
do {
doSomething();
} while (boolExpression)

ループについて

Luau のループに対する一般的なもの

-- フォワードループ
for i = 1, 10 do
doSomething()
end
-- リバースループ
for i = 10, 1, -1 do
doSomething()
end
Generic For Loops in C#

// Forward loop
for (int i = 1; i <= 10; i++) {
doSomething();
}
// Reverse loop
for (int i = 10; i >= 1; i--) {
doSomething();
}
Luau のテーブル上のループに対して

local abcList = {"a", "b", "c"}
for i, v in ipairs(abcList) do
print(v)
end
local abcDictionary = { a=1, b=2, c=3 }
for k, v in pairs(abcDictionary) do
print(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
}