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) -- 출력 '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 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#
같음 대于====
보다 크다 than>>
작은 값보다<<
보다 크거나 같음보다 크거나 같음>=>=
미만 또는 같음 이하<=<=
같지 않음을 비교하다~=!=
그리고and&&
Oror||

산술 연산자

LuauC#
추가++
빼기--
곱셈**
분할//
모듈%%
지수 함수^**

변수

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#의 굵은 괄호와 유사하게 및 키워드 내에서 논리를 중첩할 수 있습니다.자세한 내용은 범위를 참조하십시오.

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) -- prints "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의 조건부 문장

-- 하나의 조건
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 루프

루au에서 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
}