Roblox verwendet die Programmiersprache Luau. Die folgenden Codebeispiele und Tabellen zeigen einige der Unterschiede zwischen den Syntaxen für C# und Luau.
Abschlusszeilen
Du brauchst Semikolonen in Luau nicht, aber sie brechen die Syntax nicht.
Reservierte Schlüsselwörter
Die folgende Tabelle zeigt die reservierten Schlüsselwörter von Luau auf ihre C#-Äquivalente umgemappt. Beachten Sie, dass sie nicht alle C#-Schlüsselwörter zeigt.
Luau | 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 |
Kommentare
Kommentare in Luau
-- Einzelzeiliger Kommentar posten--[[ Ausgabe:Block comment--]]
Comments in C#
// Single line comment/*Block comment*/
Schnüre
Schnüre in Luau
-- Mehrzeiliger Stringlocal multiLineString = [[This is a string that,when printed, appearson multiple lines]]-- Konkatinationlocal 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;
Tabellen
Um mehr über Tabellen in Luau zu erfahren, siehe Tabellen.
Wörterbuchtabellen
Du kannst Tabellen in Luau als Wörterbücher verwenden, genau wie in C#.
Wörterbuchtabellen in Luau
local dictionary = {val1 = "this",val2 = "is"}print(dictionary.val1) -- Gibt 'this' ausprint(dictionary["val1"]) -- Gibt 'this' ausdictionary.val1 = nil -- Entfernt 'val1' aus der Tabelledictionary["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
Zahlungsindexierte Tabellen
Du kannst Tabellen in Luau als Arrays verwenden, genau wie in C#. Indizes beginnen bei 1 in Luau und 0 in C#.
Zahlungsindexierte Tabellen in Luau
local npcAttributes = {"strong", "intelligent"}print(npcAttributes[1]) -- Gibt 'strong' ausprint(#npcAttributes) -- Gibt die Größe der Liste aus-- Zur Liste hinzufügentable.insert(npcAttributes, "humble")-- Eine andere Möglichkeit...npcAttributes[#npcAttributes+1] = "humble"-- Am Anfang der Liste einfügentable.insert(npcAttributes, 1, "brave")-- Artikel bei einem bestimmten Index entfernentable.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);
Betreiber
Bedingungsoperatoren
Operator | Luau | C# |
---|---|---|
Äquivalent zu | == | == |
Größer als | > | > |
Weniger als | < | < |
Größer als oder gleich zu | >= | >= |
Weniger als oder gleich zu | <= | <= |
Nicht gleich zu | ~= | != |
Und | and | && |
Or | or | || |
Arithmetische Operatoren
Luau | C# | |
---|---|---|
Hinzufügung | + | + |
Subtraktion | - | - |
Multiplikation | * | * |
Teilung | / | / |
Modul | % | % |
Exponentialisierung | ^ | ** |
Variablen
In Luau geben Variablen bei der Erklärung nicht ihren Typ an, wenn du sie erklärst.Luau-Variablen haben keine Zugriffsmodifizierer, obwohl du "privater Server" Variablen mit einem Unterstrich für Lesbarkeit vorfixieren kannst.
Variablen in Luau
local stringVariable = "value"-- Erklärung "Öffentlich"local variableName-- „Privat“-Erklärung - wurde auf die gleiche Weise parsiertlocal _variableName
Variables in C#
string stringVariable = "value";// Public declarationpublic string variableName// Private declarationstring variableName;
Bereich
In Luau können Sie Variablen und Logik in einem engeren Umfang als ihre Funktion oder Klasse schreiben, indem Sie die Logik innerhalb von do und end Schlüsselwörtern nesten, ähnlich wie kurze Klammern {} in C#.Für weitere Details siehe Umfang.
Luau einrahmen
local outerVar = 'Outer scope text'do-- Modifizieren von 'outerVar'outerVar = 'Inner scope modified text'-- Führe eine lokale Variable einlocal innerVar = 'Inner scope text'print('1: ' .. outerVar) -- druckt "1: Innerer Umfang modifizierter Text"print('2: ' .. innerVar) -- druckt "2: Innerer Scope-Text"endprint('3: ' .. outerVar) -- druckt "3:" Änderungstext des inneren Umfangs-- 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
Bedingte Aussagen
Bedingte Aussagen in Luau
-- Eine Bedingungif boolExpression thendoSomething()end-- Mehrere Bedingungenif 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();}
Konditionale Operator
Bedingungsoperator in Luau
local max = if x > y then x else y
Conditional Operator in C#
int max = (x > y) ? x : y;
Zyklen
Um mehr über Schleifen in Luau zu erfahren, siehe Kontrollstrukturen.
Während und wiederholte Schleifen
Während und Wiederholungszyklen in Luau
while boolExpression dodoSomething()endrepeatdoSomething()until not boolExpression
While and Repeat Loops in C#
while (boolExpression) {doSomething();}do {doSomething();} while (boolExpression)
Für Schleifen
Generisch für Schleifen in Luau
-- Vorwärts-Schleifefor i = 1, 10 dodoSomething()end-- Rücklaufschleifefor 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();}
Für Schleifen über Tabellen in 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 unterstützt auch generische Iteration, was die Arbeit mit Tabellen weiter vereinfacht.
Funktionen
Um mehr über Funktionen in Luau zu erfahren, siehe Funktionen.
Generische Funktionen
Generische Funktionen in Luau
-- Generische Funktion
local function increment(number)
return number + 1
end
Generic Functions in C#
// Generic function
int increment(int number) {
return number + 1;
}
Variable Argumentanzahl
Variable Argumentanzahl in Luau
-- Variable Argumentanzahl
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);
}
}
Benannte Argumente
Benannte Argumente in Luau
-- Benannte Argumente
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");
Versuch-Fang-Strukturen
Versuche/Fange Strukturen in Luau
local function fireWeapon()
if not weaponEquipped then
error("No weapon equipped!")
end
-- Fortfahren...
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
}