Porównanie Luau i C#

*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.

Roblox używa języka programowania Luau. Poniższe przykłady kodu i tabele wskazują niektóre z różnic między słowami dla C# i Luau.

Końce linii

Nie potrzebujesz spacji w Luau, ale nie łamią one słownictwa.

Zarezerwowane słowa kluczowe

Poniższa tabela ma zarezerwowane słowa kluczowe Luau przypisane do ich równoważnika C#. Zauważ, że nie pokazuje wszystkich słów kluczowych C#.

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

Komentarze

Komentarze w Luau

-- komentowaćjednej linii
--[[ Wynikowe wyjście:
Block comment
--]]
Comments in C#

// Single line comment
/*
Block comment
*/

Sznurki

Sznurki w Luau

-- Wieloliniowa ciąg
local multiLineString = [[This is a string that,
when printed, appears
on multiple lines]]
-- Konkatenacja
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;

Tabelki

Aby dowiedzieć się więcej o tabelach w Luau, zobacz Tabelki.

Tabele słownikowe

Możesz używać tabel w Luau jako słowników tak jak w C#.

Tabele słownikowe w Luau

local dictionary = {
val1 = "this",
val2 = "is"
}
print(dictionary.val1) -- Wyświetla 'to'
print(dictionary["val1"]) -- Wyświetla 'to'
dictionary.val1 = nil -- Usuwa 'val1' ze stołu
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

Tabele numerowo indeksowane

Możesz używać tabel w Luau jako arrayów tak jak w C#. Indeksy zaczynają się od 1 w Luau i 0 w C#.

Tabele numerowo indeksowane w Luau

local npcAttributes = {"strong", "intelligent"}
print(npcAttributes[1]) -- Wyświetla "silne"
print(#npcAttributes) -- Wyświetla rozmiar listy
-- Dodaj do listy
table.insert(npcAttributes, "humble")
-- Inny sposób...
npcAttributes[#npcAttributes+1] = "humble"
-- Wstaw na początku listy
table.insert(npcAttributes, 1, "brave")
-- Usuń przedmiot w określonym indeksie
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);

Operatorzy

Operatory warunkowe

OperatorLuauC#
Równy z====
Większe niż>>
Mniej niż<<
Większe niż lub równe z>=>=
Mniej niż lub równy do<=<=
Nie równy z~=!=
A takżeand&&
Oror||

Operatory arytmetyczne

LuauC#
Dodanie++
Odejmowanie--
Mnożenie**
Dzielenie//
Moduł%%
Wykładnianie^**

Zmienne

W Luau zmienne nie określają swego typu, gdy je deklarujesz.Zmienne Luau nie mają modyfikatorów dostępu, choć możesz prefiksować zmienne "prywatne" za pomocą podświetlenia dla czytelności.

Zmienne w Luau

local stringVariable = "value"
-- Oświadczenie "publiczne"
local variableName
-- Deklaracja "prywatna" - przetworzona w ten sam sposób
local _variableName
Variables in C#

string stringVariable = "value";
// Public declaration
public string variableName
// Private declaration
string variableName;

Zakres

W Luau możesz pisać zmienne i logikę w bardziej ograniczonej skali niż ich funkcja lub klasa, poprzez umieszczenie logiki w do i end słowach kluczowych, podobnie jak w klamrach curly {} w C#.Aby uzyskać więcej szczegółów, zobacz zakres.

Składanie wniosków w Luau

local outerVar = 'Outer scope text'
do
-- Zmodyfikuj 'outerVar'
outerVar = 'Inner scope modified text'
-- Wprowadź lokalną zmienną
local innerVar = 'Inner scope text'
print('1: ' .. outerVar) -- drukuje "1: zmieniony tekst zakresu wewnętrznego"
print('2: ' .. innerVar) -- drukuje "2: Tekst zakresu wewnętrznego"
end
print('3: ' .. outerVar) -- drukuje „3:” zmodyfikowany tekst wewnętrznego zakresu
-- 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

Oświadczenia warunkowe

Oświadczenia warunkowe w Luau

-- Jedno warunek
if boolExpression then
doSomething()
end
-- Wiele warunków
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();
}

Operator warunkowy

Operator warunkowy w Luau

local max = if x > y then x else y
Conditional Operator in C#

int max = (x > y) ? x : y;

Pętle

Aby dowiedzieć się więcej o pętlach w Luau, zobacz Struktury kontroli.

Pętle while i repeat

Pętle while i repeat w Luau

while boolExpression do
doSomething()
end
repeat
doSomething()
until not boolExpression
While and Repeat Loops in C#

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

Dla pętli

Ogólne dla pętli w Luau

-- Pętla przodu
for i = 1, 10 do
doSomething()
end
-- Odwrócony cykl
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();
}
Dla pętli nad stołami w 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 wspiera również ogólną iterację, co jeszcze bardziej uprości pracę z tablicami.

Funkcje

Aby dowiedzieć się więcej o funkcjach w Luau, zobacz Funkcje.

Funkcje ogólne

Funkcje ogólne w Luau

-- Funkcja ogólna
local function increment(number)
return number + 1
end
Generic Functions in C#

// Generic function
int increment(int number) {
return number + 1;
}

Liczba argumentów zmiennych

Liczba argumentów zmiennych w Luau

-- Liczba argumentów zmiennych
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);
}
}

Nazwane argumenty

Nazwane argumenty w Luau

-- Nazwane argumenty
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");

Struktury próbne-łapania

Spróbuj/Złap struktury w Luau

local function fireWeapon()
if not weaponEquipped then
error("No weapon equipped!")
end
-- Kontynuuj...
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
}