Roblox utilizza il linguaggio di programmazione Luau. I seguenti esempi di codice e tabelle indicano alcune delle differenze tra le sintassi per C# e Luau.
Finiture di linea
Non hai bisogno di semicoloni in Luau, ma non rompono la sintassi.
Keyword riservate
La seguente tabella ha le parole chiave riservate di Luau mappate al loro equivalente C#. Nota che non mostra tutte le parole chiave C#.
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 |
Commenti
Commenti in Luau
-- Commentaredi una singola linea--[[ Output risultante:Block comment--]]
Comments in C#
// Single line comment/*Block comment*/
Stringhe
Stringhe in Luau
-- Stringa multilinealocal multiLineString = [[This is a string that,when printed, appearson multiple lines]]-- Concatenazionelocal 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;
Tavoli
Per saperne di più sulle tabelle in Luau, vedi Tabelle.
Tabelle dizionarie
Puoi usare le tabelle in Luau come dizionari proprio come in C#.
Tabelle dizionarie in Luau
local dictionary = {val1 = "this",val2 = "is"}print(dictionary.val1) -- Produce 'questo'print(dictionary["val1"]) -- Produce 'questo'dictionary.val1 = nil -- Rimuove 'val1' dalla tabelladictionary["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
Tabelle numericamente indicizzate
Puoi usare tabelle in Luau come array proprio come in C#. Gli indici iniziano a 1 in Luau e 0 in C#.
Tabelle numericamente indicizzate in Luau
local npcAttributes = {"strong", "intelligent"}print(npcAttributes[1]) -- Produce 'forte'print(#npcAttributes) -- Emette la dimensione della lista-- Aggiungi alla listatable.insert(npcAttributes, "humble")-- Un altro modo...npcAttributes[#npcAttributes+1] = "humble"-- Inserisci all'inizio della listatable.insert(npcAttributes, 1, "brave")-- Rimuovi l'elemento in un dato indicetable.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);
Operatori
Operatori condizionali
Operatore | Luau | C# |
---|---|---|
Uguale a | == | == |
Superiore a | > | > |
Meno di | < | < |
Superiore o uguale a | >= | >= |
Meno di o uguale a | <= | <= |
Non uguale a | ~= | != |
E | and | && |
Or | or | || |
Operatori aritmetici
Luau | C# | |
---|---|---|
Aggiunta | + | + |
Sottrazione | - | - |
Moltiplicazione | * | * |
Divisione | / | / |
Modalità | % | % |
Espansione | ^ | ** |
Variabili
In Luau, le variabili non specificano il loro tipo quando le dichiari.Le variabili Luau non hanno modificatori di accesso, anche se puoi preфиксиare le variabili "Server privato" con un underscore per leggibilità.
Variabili in Luau
local stringVariable = "value"-- Dichiarazione "Pubblica"local variableName-- Dichiarazione "Privata" - analizzata allo stesso modolocal _variableName
Variables in C#
string stringVariable = "value";// Public declarationpublic string variableName// Private declarationstring variableName;
Ambito
In Luau, puoi scrivere le variabili e la logica in un ambito più stretto della loro funzione o classe impostando la logica all'interno di do e end parole chiave, simili ai curly brackets {} in C#.Per maggiori dettagli, vedi Ambito .
Selezione in Luau
local outerVar = 'Outer scope text'do-- Modifica 'outerVar'outerVar = 'Inner scope modified text'-- Introduci una variabile localelocal innerVar = 'Inner scope text'print('1: ' .. outerVar) -- prints "1: Testo modificato a livello interno"print('2: ' .. innerVar) -- prints "2: Testo di scopo interno"endprint('3: ' .. outerVar) -- prints "3:" Testo modificato a livello interno"-- 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
Statistiche condizionali
Statistiche condizionali in Luau
-- Una condizioneif boolExpression thendoSomething()end-- Condizioni multipleif 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();}
Operatore condizionale
Operatore condizionale in Luau
local max = if x > y then x else y
Conditional Operator in C#
int max = (x > y) ? x : y;
Loop
Per saperne di più sui loop in Luau, vedi Strutture di controllo.
Mentre e ripetere i loop
Cicli While e Repeat in Luau
while boolExpression dodoSomething()endrepeatdoSomething()until not boolExpression
While and Repeat Loops in C#
while (boolExpression) {doSomething();}do {doSomething();} while (boolExpression)
Per i cicli
Generico per i cicli in Luau
-- Ciclo forward loopfor i = 1, 10 dodoSomething()end-- Ciclo inversofor 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();}
Per i cicli su tabelle 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 supporta anche iterazione generalizzata, che rende ancora più semplice lavorare con le tabelle.
Functioni
Per saperne di più sulle funzioni in Luau, vedi Funzioni .
Funzioni generiche
Funzioni generiche in Luau
-- Funzione generica
local function increment(number)
return number + 1
end
Generic Functions in C#
// Generic function
int increment(int number) {
return number + 1;
}
Numero argomento variabile
Numero di argomenti variabili in Luau
-- Numero argomento variabile
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);
}
}
Argomenti nominati
Argomenti nominati in Luau
-- Argomenti nominati
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");
Strutture di cattura di prova
Prova/Cattura Strutture in Luau
local function fireWeapon()
if not weaponEquipped then
error("No weapon equipped!")
end
-- Procedere...
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
}