
I already briefly touched on the if statement. Now we can start doing some more complicated tasks than arithmetic. For example, you cannot name your variables end, if, for, or in, because all of these serve other purposes in Lua. Also, you cannot use any Lua keywords as variable names. The first character in a variable name must be a letter or the underscore. A variable name can contain letters, numbers, and the underscore, but no spaces. The last thing I have to say about variables concerns naming them. "failed to covert to number") to determine whether foo is a valid number or not. For example, you can see whether tonumber(foo) returns nil (i.e. You might think having nil values is an error that should be avoided, but nil values can be very useful.

Uninitiated variables and failed function calls will generally result in nil. If a variable is nil, it means "no valid data is stored in this variable". This is not the same as the number 0, nor is it the same as a string with no text in it. Lua has a special value called nil that can be stored in any variable. Changing the value of x on line 4 does not affect y or z. Keeping in mind that the equals sign is the assignment operator, note that at runtime, the expressions on the right are evaluated and their numeric results are stored in the variables. In Lua, multiplication is *, division is /, and exponentiation is ^. If you are working with number variables, you can perform arithmetic on them normally. To check for equality, use count = 5, which means "count is equal to 5". The expression count = 5 means "store the value 5 in count", which is not what we want here. That is, it is a command that means "store this value in this variable". Instead, the single equals sign represents assignment. In many programming languages, including Lua, the single equals sign does not mean "is equal to". Conditions can be combined using and and or. "=" mean "less than or equal to" and "greater than or equal to", respectively. The greater than and less than sign mean what you expect. The following are examples of boolean expressions. In most cases, you will be using Boolean expressions that can be true or false depending on the situation. You can use Booleans to evaluate if conditions or loops. If you want to type a normal backslash, type " \\".Īnother data type is the Boolean, which can only have two values: true or false. Note that the backslash ( \) is an "escape character" ( more info here).

In Lua, you can join one string to another using the. They behave similarly to the variables in algebra, but can store any kind of data, not just numbers.Īnother simple data type in addition to numbers is the string, which represents text and is defined with quotes.

Variables, data types, arithmetic, and logicĪ variable is the name of the place where you store data.

You can find many good resources for learning Lua on the page. On the advice of some people who helped proofread this tutorial, I've vastly condensed this section, as it is not the main focus. This section will briefly introduce basic programming concepts using Lua. I recommend frequently referencing the documentation provided in the Aegisub user manual. If you already know Lua, or if you'd rather start writing macros right away and learn Lua as you go, then you can skip to "The Aegisub Environment". If you already know a programming language (HTML doesn’t count), you can skip to "Lua for Experienced Programmers". The next section will cover basic programming concepts for people who have never programmed before. This tutorial should provide a solid starting point, but advanced users will have to do their own research. There are many features of the Lua language and the Aegisub API that I've never used and won't cover. This tutorial is based on my own knowledge and experience. In most cases, these tasks can be automated using macros written in Lua code, and often quite easily. If you’ve worked on an advanced substation alpha script, especially if you’re a typesetter, you’ve probably encountered tasks that are tedious, repetitive, and/or require more calculations than you’re willing to do. This tutorial is meant to serve as a basic guide to creating Lua automations for use in Aegisub. Creating Lua Automation Scripts for Aegisub
