Types
Variables have different types, such as string, bool, number, nil or its a table object. Since Lunaris a dynamic programming language, the type of a variable can be changed.
String
// Both single & double quotes can be used
message = "Hello World";
A string represents text and is declared by enclosing anything with a double quotation (“ “)
Bool
isNpcAlive = true;
isNpcAlive = false;
A boolean reprents either true or false. You can assign a boolean value by using the true/false keywords, or by an expression.
Number
a = 12.5;
b = 7.5;
c = a + b; // 20 (12.5 + 7.5)
A number represents a decimal numeric value.
Nil
local message = nil;
A nil represents that the variable does not have any value. In other language this is sometimes also called null or undefined.
Table
A table is a general-purpose container. You can see it as a collection of values organized in a single container.
// Creating an empty table
myTable = {
Message = "Hello";
};
// Prints "Hello" in the console
print(myTable.Message);
// We can override the Message from the tablee
myTable.Message = 'Hello World';
// Prints 'Hello World' in the console
print(myTable.Message)
// The "Message" property doesn't exist anymore now
myTable.Message = nil;
// Prints 'Nil" in the console, since Message doesn't exist anymore
print(myTable.Message)
Array
An array is a table that is used like a list. Use it when you want to have a collection of variables.
local items = ["Sword", "Shield", "Potion"]
print(items[1]) // Shield
print(items[2]) // Potion
Class
A class is a table used as a type definition. It groups data and behavior together so you can create instances from it.
class Character {
Name = "Unknown"
Health = 100
PrintInfo() {
print(this.Name)
print(this.Health)
}
}
var hero = new Character()
hero.PrintInfo() // Does 2 prints: "Unknown" & "100"
hero.Name = "Aria"
hero.PrintInfo() // Does 2 prints: "Aria" & "100"
Enum
An enum is a table used for named constant values. Instead of using random numbers, you can use an enum to have readable labels instead of numbers.
enum DamageType {
Physical, // 0
Fire, // 1
Ice // 2
}
print(DamageType.Physical) // prints "0"
print(DamageType.Fire) // prints "1"