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) -- 출력 '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 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# |
---|---|---|
같음 대于 | == | == |
보다 크다 than | > | > |
작은 값보다 | < | < |
보다 크거나 같음보다 크거나 같음 | >= | >= |
미만 또는 같음 이하 | <= | <= |
같지 않음을 비교하다 | ~= | != |
그리고 | and | && |
Or | or | || |
산술 연산자
Luau | C# | |
---|---|---|
추가 | + | + |
빼기 | - | - |
곱셈 | * | * |
분할 | / | / |
모듈 | % | % |
지수 함수 | ^ | ** |
변수
Luau에서 변수는 선언할 때 유형을 지정하지 않습니다.루오 변수에는 액세스 수정자가 없지만, 가독성을 위해 밑줄로 "비공개" 변수를 접두사로 사용할 수 있습니다.
Luau의 변수
local stringVariable = "value"-- “공개” 선언local variableName-- “개인” 선언 - 동일한 방식으로 분석local _variableName
Variables in C#
string stringVariable = "value";// Public declarationpublic string variableName// Private declarationstring 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: 내부 범위 텍스트"endprint('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 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 및 repeat 루프
루au에서 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
}