<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://tw-modding.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=MrIncongruous</id>
	<title>Total War Modding - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://tw-modding.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=MrIncongruous"/>
	<link rel="alternate" type="text/html" href="https://tw-modding.com/wiki/Special:Contributions/MrIncongruous"/>
	<updated>2026-05-14T06:19:32Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://tw-modding.com/index.php?title=Lua:Keywords&amp;diff=412</id>
		<title>Lua:Keywords</title>
		<link rel="alternate" type="text/html" href="https://tw-modding.com/index.php?title=Lua:Keywords&amp;diff=412"/>
		<updated>2021-07-22T06:17:14Z</updated>

		<summary type="html">&lt;p&gt;MrIncongruous: Explained what a keyword is&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A keyword is a special word that has been taken for the program, so we can't use it in our own as the name of a variable, function, etc.&lt;br /&gt;
&lt;br /&gt;
We’ve covered the majority of the Lua keywords, but there are a few left that we have to cover. We’ll cover the final keywords that aren’t loop/table-based in this tutorial, and we’ll cover tables and loops in the next! We only have three to touch on.&lt;br /&gt;
&lt;br /&gt;
==== If, Elseif, Else ====&lt;br /&gt;
So, we’ve seen conditional statements – if these things, then do this. But we haven’t covered their full usage!&lt;br /&gt;
&lt;br /&gt;
First things first – if you want stuff to happen based on a condition, you use that word I just used twice – &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
     &amp;lt;code&amp;gt;local var = 5&lt;br /&gt;
 &lt;br /&gt;
     if var == 5 then&lt;br /&gt;
         -- &amp;lt;nowiki&amp;gt;[[ do stuff ]]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
     end&amp;lt;/code&amp;gt;&lt;br /&gt;
Note that &amp;lt;code&amp;gt;then&amp;lt;/code&amp;gt; defines a new scope here, and that scope ends at, well, &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt;. The conditional is the statement between &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;then&amp;lt;/code&amp;gt;.&lt;br /&gt;
     &amp;lt;code&amp;gt;local var = 5&lt;br /&gt;
 &lt;br /&gt;
     function example()&lt;br /&gt;
         if var == 5 then -- if var is equivalent to 5 then...&lt;br /&gt;
             out(&amp;quot;This is 5!&amp;quot;)&lt;br /&gt;
             return&lt;br /&gt;
         end&lt;br /&gt;
         out(&amp;quot;Not 5!&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     example() → This is 5!&lt;br /&gt;
 &lt;br /&gt;
     var = 10&lt;br /&gt;
 &lt;br /&gt;
     example() → Not 5!&amp;lt;/code&amp;gt;&lt;br /&gt;
In this example, we’re checking whether var is &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;. If it is, we print “This is 5!”; if it isn’t, we print “Not 5!”. We prevent the printing of ''both'' of them by using the &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; – otherwise, if the var were &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;, ''both'' outs would be called.&lt;br /&gt;
&lt;br /&gt;
We can also do this by using the &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; keyword:&lt;br /&gt;
     &amp;lt;code&amp;gt;local var = 5&lt;br /&gt;
 &lt;br /&gt;
     function example()&lt;br /&gt;
         if var == 5 then&lt;br /&gt;
             out(&amp;quot;This is 5!&amp;quot;)&lt;br /&gt;
         else&lt;br /&gt;
             out(&amp;quot;Not 5!&amp;quot;)&lt;br /&gt;
         end&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     example() → This is 5!&lt;br /&gt;
 &lt;br /&gt;
     var = 10&lt;br /&gt;
 &lt;br /&gt;
     example() → Not 5!&amp;lt;/code&amp;gt;&lt;br /&gt;
Functionally, exactly the same. However, it’s prettier to look at, and makes more sense to read. Do note that &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; also creates its own scope; in this instance, there is a scope between &amp;lt;code&amp;gt;then&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt;; and another between &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
What if we wanted to check for more than one specific condition? We can use &amp;lt;code&amp;gt;elseif&amp;lt;/code&amp;gt;!&lt;br /&gt;
     &amp;lt;code&amp;gt;local var = 5&lt;br /&gt;
 &lt;br /&gt;
     function example()&lt;br /&gt;
         if var == 5 then&lt;br /&gt;
             out(&amp;quot;This is 5!&amp;quot;)&lt;br /&gt;
         elseif var == 7 then&lt;br /&gt;
             out(&amp;quot;This is 7!&amp;quot;)&lt;br /&gt;
         else&lt;br /&gt;
             out(&amp;quot;Not 5 or 7!&amp;quot;)&lt;br /&gt;
         end&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     example() → This is 5!&lt;br /&gt;
 &lt;br /&gt;
     var = 7&lt;br /&gt;
 &lt;br /&gt;
     example() → This is 7!&lt;br /&gt;
 &lt;br /&gt;
     var = 10&lt;br /&gt;
 &lt;br /&gt;
     example() → Not 5 or 7!&amp;lt;/code&amp;gt;&lt;br /&gt;
We can use more than one elseif, too. Only one &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; within that block between &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; and the first &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt;, though.&lt;br /&gt;
&lt;br /&gt;
Do keep in mind that whichever “path” works first is the one that will be used, and the ones following it will be ignored, until the &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt; of the conditional.&lt;br /&gt;
     &amp;lt;code&amp;gt;local var = 10&lt;br /&gt;
 &lt;br /&gt;
     function example()&lt;br /&gt;
         if var &amp;gt; 5 then&lt;br /&gt;
             out(&amp;quot;This is greater than 5!&amp;quot;)&lt;br /&gt;
         elseif var == 5 then&lt;br /&gt;
             out(&amp;quot;This is 5!&amp;quot;)&lt;br /&gt;
         elseif var == 7 then&lt;br /&gt;
             out(&amp;quot;This is 7!&amp;quot;)&lt;br /&gt;
         else&lt;br /&gt;
             out(&amp;quot;Less than 5, not 7!&amp;quot;)&lt;br /&gt;
         end&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     example() → This is greater than 5!&lt;br /&gt;
 &lt;br /&gt;
     var = 5&lt;br /&gt;
 &lt;br /&gt;
     example() → This is 5!&lt;br /&gt;
 &lt;br /&gt;
     var = 7&lt;br /&gt;
 &lt;br /&gt;
     example() → Less than 5, not 7!&amp;lt;/code&amp;gt;&lt;br /&gt;
Even though the var matches the second conditional statement – &amp;lt;code&amp;gt;var == 7&amp;lt;/code&amp;gt; – for the final assignment, it also matches the first condition, so that’s the one and only path the code takes here.&lt;br /&gt;
&lt;br /&gt;
Do note that you can do just one if and elseif, or an if and an else, or all three, or just an if, or like twenty elseif’s. Though that’s bad code, don’t do that.&lt;br /&gt;
&lt;br /&gt;
==== End, Then, Do ====&lt;br /&gt;
The keyword &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt; is needed to denote the end of a scope, when that scope is defined by something other than the entire file. Each of the following keywords need an &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt; to finish their scope:&lt;br /&gt;
&lt;br /&gt;
* then&lt;br /&gt;
* do&lt;br /&gt;
* function&lt;br /&gt;
&lt;br /&gt;
And the keyword &amp;lt;code&amp;gt;then&amp;lt;/code&amp;gt; is needed after the conditional statement for an &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;elseif&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;code&amp;gt;do&amp;lt;/code&amp;gt; is used in two instances; one, to create a new scope without needing any other keywords (see example below).&lt;br /&gt;
 &lt;br /&gt;
     &amp;lt;code&amp;gt;local var = 5&lt;br /&gt;
 &lt;br /&gt;
     do&lt;br /&gt;
         local var = 10&lt;br /&gt;
         out(var) → 10&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     out(var) → 5&amp;lt;/code&amp;gt;&lt;br /&gt;
The other, and more common, usage of &amp;lt;code&amp;gt;do&amp;lt;/code&amp;gt; is to attach it to loop keywords. Read on to the next chapter!&lt;br /&gt;
[[Category:Lua Tutorials]]&lt;/div&gt;</summary>
		<author><name>MrIncongruous</name></author>
	</entry>
	<entry>
		<id>https://tw-modding.com/index.php?title=Lua:Variables_%26_Types&amp;diff=413</id>
		<title>Lua:Variables &amp; Types</title>
		<link rel="alternate" type="text/html" href="https://tw-modding.com/index.php?title=Lua:Variables_%26_Types&amp;diff=413"/>
		<updated>2021-07-22T06:06:26Z</updated>

		<summary type="html">&lt;p&gt;MrIncongruous: small changes to make it clearer for people who have no previous experience programming.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
''NOTE: I recommend you have the Devtool Console handy to mess around with some of the stuff covered here.''&lt;br /&gt;
&lt;br /&gt;
Following the concept of chunks and statements, the next most important subject to cover is variables, and types.&lt;br /&gt;
&lt;br /&gt;
Variable: A custom-made keyword that can be used to represent a value&lt;br /&gt;
&lt;br /&gt;
Type: The sort of value that is in a variable. Lua types include: boolean, string, number, table, nil, function. There are more, but we won’t need them.&lt;br /&gt;
&lt;br /&gt;
But before going deeper into a definition, let’s look at an example:&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 10&lt;br /&gt;
 b = 5&lt;br /&gt;
 c = a + b&amp;lt;/code&amp;gt;&lt;br /&gt;
We can see that each of those letters in the chunk above is a variable, which are each assigned a value. In this case, variable &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; is assigned the value &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt;, variable &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is assigned the value &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;, and the variable &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; is assigned the &amp;lt;code&amp;gt;value of a plus the value of b&amp;lt;/code&amp;gt;. This instance (c = a + b) works a lot like algebra – you aren’t making “c = ab”, but you’re adding the value of a (10) to the value of b (5), to get the result of 15.&lt;br /&gt;
&lt;br /&gt;
The coolest thing about variables is their dynamism. They can be assigned to any type, change to any type, and they can infinitely change values. Let’s take the above code, and mess with it a little bit.&lt;br /&gt;
&lt;br /&gt;
''NOTE: From this point on, you’ll see → after out() calls. This is to specify what text is actually output.''&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 10&lt;br /&gt;
 b = 5&lt;br /&gt;
 c = a + b&lt;br /&gt;
 &lt;br /&gt;
 out(a) → 10&lt;br /&gt;
 out(b) → 5&lt;br /&gt;
 out(c) → 15&lt;br /&gt;
 &lt;br /&gt;
 a = 25&lt;br /&gt;
 out(a) → 25&lt;br /&gt;
 &lt;br /&gt;
 out(c) → 15&lt;br /&gt;
 &lt;br /&gt;
 c = a + b&lt;br /&gt;
 out(c) → 30&amp;lt;/code&amp;gt;&lt;br /&gt;
I wanted to point out an important thing with this example. If a variable is assigned to the values of some other variables (c = a + b), it will do the calculation and assignment the moment that the statement is called. If either variable (a or b) is changed after the assignment statement, the assigned variable’s (c) value (15) won’t change. It takes the ''variable’s value'', not the ''variable itself''.&lt;br /&gt;
&lt;br /&gt;
So, we see here that variables can be numbers, which is one of the primary types in Lua.&lt;br /&gt;
&lt;br /&gt;
Number: ''any'' numerical value, within a specific number range (a much bigger number than needs to be worried about)&lt;br /&gt;
&lt;br /&gt;
As we saw above, we can perform some basic arithmetic on numbers.&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 5&lt;br /&gt;
 b = 2 - 0.34&lt;br /&gt;
 c = a + b&lt;br /&gt;
 d = c * 2&lt;br /&gt;
 e = d / 3&lt;br /&gt;
 &lt;br /&gt;
 out(c) → 7.56&lt;br /&gt;
 out(d) → 15.12&lt;br /&gt;
 out(e) → 5.04&amp;lt;/code&amp;gt;&lt;br /&gt;
String: ''any'' alphanumerical value, within quotation marks (“” or ”). Similar to numbers, size ''does not matter'', Lua can handle millions of characters within a string.&lt;br /&gt;
&lt;br /&gt;
Like arithmetic for numbers, one can concatenate strings (along with many other stuffs!) to combine several, using the &amp;lt;code&amp;gt;..&amp;lt;/code&amp;gt; operator.&lt;br /&gt;
 &amp;lt;code&amp;gt;a = “This “; b = “is “; c = “an “; d = “example.”&lt;br /&gt;
 out(a .. b .. c .. d) → This is an example.&amp;lt;/code&amp;gt;&lt;br /&gt;
''NOTE: Yes, you can have several statements on the same line in Lua! The semi-colons are not necessary, I added them for extra readability.''&lt;br /&gt;
&lt;br /&gt;
While on the subject, you can concat (cool-kid for concatenate) string literals and variables.&lt;br /&gt;
 &amp;lt;code&amp;gt;a = &amp;quot;example&amp;quot;&lt;br /&gt;
 out(&amp;quot;This is an &amp;quot; .. a) → This is an example&amp;lt;/code&amp;gt;&lt;br /&gt;
Boolean: a &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; value.&lt;br /&gt;
 &amp;lt;code&amp;gt;a = false&lt;br /&gt;
 out(a) → false&lt;br /&gt;
 out(a == false) → true&amp;lt;/code&amp;gt;&lt;br /&gt;
The above probably looks a little weird, but that’s because we’re learning a new operator! The &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt; operator checks for ''equality'', whereas the &amp;lt;code&amp;gt;=&amp;lt;/code&amp;gt; operator ''assigns a value''. The final statement above reads “output the result of a == false”. Since &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; DOES equal false, it prints “true”.&lt;br /&gt;
&lt;br /&gt;
It’s very important to note that any variable can be ''treated'' as a boolean, such as when using the “if something then” statement, or by comparing it to a boolean. If a non-boolean variable is treated as a boolean, it will be converted to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; if the value is &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;; otherwise, it will be converted to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;.&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 0&lt;br /&gt;
 b = nil&lt;br /&gt;
 out(a) → 0&lt;br /&gt;
 out(a == false) → false &lt;br /&gt;
 out(b == false) → true&amp;lt;/code&amp;gt;&lt;br /&gt;
In this case, &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; does NOT equal &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;, because it has a value to it that isn’t &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt;. By the same card, &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; DOES equal &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You’re probably asking by now, what IS nil? I’ve mentioned it a number of times already, you’re probably ready to lear about it. Here we go!&lt;br /&gt;
&lt;br /&gt;
Nil: the absence of data.&lt;br /&gt;
&lt;br /&gt;
If one were to try to print a ''non-existent'' variable, the result would be &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt;. That’s because there is no data for that variable.&lt;br /&gt;
 &amp;lt;code&amp;gt;out(a) → nil&lt;br /&gt;
 a = 5&lt;br /&gt;
 out(a) → 5&amp;lt;/code&amp;gt;&lt;br /&gt;
Nil is a good way to check whether or not something exists, and we’ll, towards the bottom of this lesson, talk about how to type-check this data in our scripts to see if variables are NOT nil, and thus exist.&lt;br /&gt;
&lt;br /&gt;
Nil is also a good way to clear away variables that you don’t want to use anymore. There’s a more realistic way of doing it, and this probably won’t be necessary for most of what you will be doing, but I like to give a full knowledge.&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 5&lt;br /&gt;
 out(a) → 5&lt;br /&gt;
 a = nil -- Memory deallocated, bye &amp;quot;a&amp;quot;!&lt;br /&gt;
 out(a) → nil&amp;lt;/code&amp;gt;&lt;br /&gt;
Lastly, it’s important to note that &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt; is not the same as &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;, though they look quick similar. We’ll cover that soon-ish, when we get into more CA script interfaces (like FACTION_SCRIPT_INTERFACE that we saw a few lessons ago).&lt;br /&gt;
&lt;br /&gt;
Function: a definition for a chunk that can be called at any point, after being defined.&lt;br /&gt;
&lt;br /&gt;
There are two types of functions – an anonymous function, and a named function. An anonymous function has no name (clearly), so it cannot be called further on in the script. We use anonymous functions relatively frequently, but our focus now will be on named functions, which are functions assigned to a variable.&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 10&lt;br /&gt;
 example = out&lt;br /&gt;
 example(a) → 10&amp;lt;/code&amp;gt;&lt;br /&gt;
Yes, that’s valid! You can actually assign a variable to a function like, it’s pretty cool! We have syntactic sugar for assigning a variable to a function, however, and it’s more common to see script like that:&lt;br /&gt;
 &amp;lt;code&amp;gt;function example(text)&lt;br /&gt;
     out(text)&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 example(&amp;quot;Testing!&amp;quot;) → Testing!&lt;br /&gt;
 &lt;br /&gt;
 example(&amp;quot;A Cooler Test!&amp;quot;) → A Cooler Test!&amp;lt;/code&amp;gt;&lt;br /&gt;
As an added bonus, we’re getting a quick sneak peak at one of the upcoming tutorials: parameters and arguments, for functions. In the above function, “text” is a parameter for &amp;lt;code&amp;gt;example()&amp;lt;/code&amp;gt;, which is then passed along to &amp;lt;code&amp;gt;out&amp;lt;/code&amp;gt; and triggers that function within the chunk.&lt;br /&gt;
&lt;br /&gt;
Table: a collection of more than one piece of data.&lt;br /&gt;
&lt;br /&gt;
There are several types of tables, which we’ll cover later on. I’d like to first look at the ''array'' version of tables, the simplest and most common.&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 5&lt;br /&gt;
 b = 10&lt;br /&gt;
 c = 17&lt;br /&gt;
 &lt;br /&gt;
 table = {&lt;br /&gt;
     a,&lt;br /&gt;
     b,&lt;br /&gt;
     c&lt;br /&gt;
 } -- assign the three *values* of the variables to the table&lt;br /&gt;
     &lt;br /&gt;
 out(table[1]) → 5 -- Lua tables start at index 1: we're showing the first element of our table, that is, the item on index 1!&lt;br /&gt;
 out(table[2]) → 10&lt;br /&gt;
 out(table[3]) → 17&lt;br /&gt;
 out(table[4]) → nil -- doesn't exist!&amp;lt;/code&amp;gt;&lt;br /&gt;
''NOTE for programmers:'' ''Arrays in Lua start at index ONE, not index ZERO. &amp;lt;code&amp;gt;table[0]&amp;lt;/code&amp;gt; would be invalid in the above example! If you get into other programming languages, you'll understand why explaining this was important.''&lt;br /&gt;
&lt;br /&gt;
Arrays are numbered automatically, so it really reads “the first index of table is 5, the second index of table is 10”, and so on. You ''access'' those indexes by using the &amp;lt;code&amp;gt;[num\]&amp;lt;/code&amp;gt; operator, which we’ll look further at in the next tables tutorial later on. The reason this works, however, is because Lua is automatically numbering these values, giving them a ''key''. When you make an array like that, Lua reads it more like the following:&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 5&lt;br /&gt;
 b = 10&lt;br /&gt;
 c = 17&lt;br /&gt;
 &lt;br /&gt;
 table = {&lt;br /&gt;
     [1] = a,&lt;br /&gt;
     [2] = b,&lt;br /&gt;
     [3] = c&lt;br /&gt;
 }&amp;lt;/code&amp;gt;&lt;br /&gt;
The other major type of tables is a map, which works by manually assigning the key of a value instead of letting Lua automatically number them, like in arrays.&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 5&lt;br /&gt;
 b = 10&lt;br /&gt;
 c = 15&lt;br /&gt;
 &lt;br /&gt;
 table = {&lt;br /&gt;
     [&amp;quot;five&amp;quot;] = a,&lt;br /&gt;
     [&amp;quot;ten&amp;quot;] = b,&lt;br /&gt;
     [&amp;quot;fifteen&amp;quot;] = c&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 out(table[&amp;quot;five&amp;quot;]) → 5&lt;br /&gt;
 out(table[&amp;quot;ten&amp;quot;]) → 10&lt;br /&gt;
 out(table[&amp;quot;fifteen&amp;quot;]) → 15&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Maps will come into some great use pretty soon, but for now I just wanted to introduce you to how they look.&lt;br /&gt;
&lt;br /&gt;
For now, you’ve done well, and I haven’t even challenged you yet!&lt;br /&gt;
&lt;br /&gt;
==== Challenge 1 ====&lt;br /&gt;
Go to repl.it (covered in the Foreword), and mess with all this stuff! Use &amp;lt;code&amp;gt;print(whatever)&amp;lt;/code&amp;gt;, where whatever is what you want to print, in lieu of &amp;lt;code&amp;gt;out()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Type-Checking ====&lt;br /&gt;
And before we say goodbye, let’s look at a few more CA functions. These ones can type-check for us, seeing if a variable is, for instance, a string.&lt;br /&gt;
&lt;br /&gt;
The list is as follows:&lt;br /&gt;
&lt;br /&gt;
* is_nil(variable)&lt;br /&gt;
* is_string(variable)&lt;br /&gt;
* is_number(variable)&lt;br /&gt;
* is_boolean(variable)&lt;br /&gt;
* is_function(variable)&lt;br /&gt;
* is_table(variable)&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;a = 5&lt;br /&gt;
 b = nil&lt;br /&gt;
 c = {1, 2, 3, 5, 70}&lt;br /&gt;
 function d() end&lt;br /&gt;
 &lt;br /&gt;
 out(is_number(a)) → true    &lt;br /&gt;
 out(is_nil(b)) → true&lt;br /&gt;
 out(is_table(c)) → true&lt;br /&gt;
 out(is_function(d)) → true&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>MrIncongruous</name></author>
	</entry>
	<entry>
		<id>https://tw-modding.com/index.php?title=Lua:Main_Page&amp;diff=414</id>
		<title>Lua:Main Page</title>
		<link rel="alternate" type="text/html" href="https://tw-modding.com/index.php?title=Lua:Main_Page&amp;diff=414"/>
		<updated>2021-07-22T05:54:43Z</updated>

		<summary type="html">&lt;p&gt;MrIncongruous: itmakes much more sense to have variables and types before expressions and operators, since E&amp;amp;O assumes knowledge of, for example, what a string is, and so on.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Howdy, internet stranger, and welcome. My name’s Vandy, and I’ll be guiding you through this new chapter of your life. This section of the Total War Modding Resources is dedicated to the learning and appreciation of the Lua language.&lt;br /&gt;
&lt;br /&gt;
==== What Is Lua? ====&lt;br /&gt;
One of the primary types of modding, in Total War, is the use of the programming language &amp;lt;code&amp;gt;Lua&amp;lt;/code&amp;gt;. In case you were wondering, Lua is Portugese for ‘moon’, and the dude who made the language was born in 1960, and at the time of this writing the language is only 26 years old.&lt;br /&gt;
&lt;br /&gt;
Using Lua, you can establish chains of events, create flexibility on when units can be recruited or when lords are spawned, and you can set up new quest battles. The entire Chaos Invasion mechanic from WH2 is scripted; from 3K, the randomly-spawned ancillaries from master-craftsmen buildings are scripted.&lt;br /&gt;
&lt;br /&gt;
Unlike db entries, which are compiled during the initial loading screen of the game and turned into engine data, and are often static with few exceptions, Lua is a dynamic energy within the game, and is the only direct way to access a multitude of game features, background data, and more.&lt;br /&gt;
&lt;br /&gt;
==== From Poop To OOP ====&lt;br /&gt;
From here to the end of this series, the goal is to get you from writing poop – or, in some of your cases, absolutely nothing – to writing in OOP, like a true-blooded programmer, though that’s by no means necessary and you can stop any time prior to that.&lt;br /&gt;
&lt;br /&gt;
To begin, I’m going to take you on a quick field-trip as we download some new software that will help us write better code using smart programs, and we’ll talk about some really easy ways to test Lua code.&lt;br /&gt;
&lt;br /&gt;
After that, our next journey is through the Lua basics. We’ll learn about strings, and booleans, and functions, and tables, and loops, and return values. All of the basic details of the knowledge that are necessary for even the simplest scripts.&lt;br /&gt;
&lt;br /&gt;
Next, we turn to Total War specifics. There are some concepts that we need to cover while writing Lua in Total War, such as events, interfaces, when to call your scripts, stuff like that.&lt;br /&gt;
&lt;br /&gt;
Towards the end, I’ll progress from simple scripting to some more high-minded scripting, using what the biz refers to as “Object-Oriented Programming”. It’s a more advanced topic that won’t do well being talked about here, but getting a firm grasp on it will help propel your scripting to a new level.&lt;br /&gt;
&lt;br /&gt;
All throughout this series, I’ll seek to keep the information as game-agnostic as possible, I’ll seek to cover all gaps of knowledge and point out any common troubleshooting tips where necessary, and I’ll be covering these topics in the order of which I believe they should be learned. It is highly recommended you don’t skip around or skip ahead, the order of these tutorials are deliberate.&lt;br /&gt;
&lt;br /&gt;
=== Order ===&lt;br /&gt;
[[Lua:Chunks &amp;amp; Statements]]&lt;br /&gt;
&lt;br /&gt;
[[Lua:Variables &amp;amp; Types]]&lt;br /&gt;
&lt;br /&gt;
[[Lua:Expressions &amp;amp; Operators]]&lt;br /&gt;
&lt;br /&gt;
[[Lua:Scopes &amp;amp; Stuff]]&lt;br /&gt;
&lt;br /&gt;
[[Lua:Keywords]]&lt;br /&gt;
&lt;br /&gt;
[[Lua:Tables &amp;amp; Loops]]&lt;br /&gt;
&lt;br /&gt;
[[Lua:Functions, Arguments &amp;amp; Parameters (Also Returns)]]&lt;br /&gt;
&lt;br /&gt;
[[Lua:Commands, Interfaces &amp;amp; Listeners]]&lt;br /&gt;
&lt;br /&gt;
[[Lua:Calling Scripts]]&lt;br /&gt;
&lt;br /&gt;
[[Lua:Advanced Conditionals]]&lt;/div&gt;</summary>
		<author><name>MrIncongruous</name></author>
	</entry>
	<entry>
		<id>https://tw-modding.com/index.php?title=Lua:Expressions_%26_Operators&amp;diff=415</id>
		<title>Lua:Expressions &amp; Operators</title>
		<link rel="alternate" type="text/html" href="https://tw-modding.com/index.php?title=Lua:Expressions_%26_Operators&amp;diff=415"/>
		<updated>2021-07-22T05:53:18Z</updated>

		<summary type="html">&lt;p&gt;MrIncongruous: added a few explanations that don't appear on the &amp;quot;chunks and statements&amp;quot; section, which goes right before this one&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
As we’ve seen, Lua is made up of &amp;lt;code&amp;gt;chunks&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;statements&amp;lt;/code&amp;gt;. Statements can contain variables, and all variables have a type, though it can change at any point.&lt;br /&gt;
&lt;br /&gt;
Expressions are the section of a statement that, essentially, tell Lua ''what to do''. They come in a billion different shapes and sizes. For now, we’ll cover all the basics you’ll need, and some basics you really don’t need but I’ll hand it to you anyway, and we’ll go back and review some older stuff. Here we go!&lt;br /&gt;
&lt;br /&gt;
==== Math Operators ====&lt;br /&gt;
Sweet. This is what all of those quizzes in grade school trained you for. We have the big four from algebra – addition, subtraction, multiplication, and division.&lt;br /&gt;
     &amp;lt;code&amp;gt;out(2 + 3) → 5&lt;br /&gt;
     out(5 - 1) → 4&lt;br /&gt;
     out(16 * 3) → 48&lt;br /&gt;
     out(9 / 3) → 3&amp;lt;/code&amp;gt;&lt;br /&gt;
We can also use a &amp;lt;code&amp;gt;-&amp;lt;/code&amp;gt; to ''negate'' a value, or, in simple terms, turn a positive number into a negative and vice versa.&lt;br /&gt;
     &amp;lt;code&amp;gt;a = 10&lt;br /&gt;
     b = -20&lt;br /&gt;
     out( -a ) → -10&lt;br /&gt;
     out( -b ) → 20&amp;lt;/code&amp;gt;&lt;br /&gt;
We also have a couple other fun math things. We can raise numbers to any power, using an exponential expression.&lt;br /&gt;
     &amp;lt;code&amp;gt;a = 2&lt;br /&gt;
     b = 4&lt;br /&gt;
     c = 3&lt;br /&gt;
     out( a^a ) → 4&lt;br /&gt;
     out( b^c ) → 64&lt;br /&gt;
     out( 64^0.5 ) → 8 -- getting the square root of a number!&amp;lt;/code&amp;gt;&lt;br /&gt;
We can also use some modulo math. It’s a way of dividing two numbers that gives you the amount ''remaining'', and it’s a great way to read if a number is a ''multiple'' of another.&lt;br /&gt;
     &amp;lt;code&amp;gt;out(8 % 2) → 0 -- 8/2 = 4, and divides evenly, so nothing is remaining!&lt;br /&gt;
     out(7 % 2) → 1 -- 7/2. &amp;quot;6/2&amp;quot; divides evenly, so we subtract 6 from 7, and get &amp;quot;1&amp;quot; as the remainder!&lt;br /&gt;
     out(8 % 3) → 2 -- 8/3. &amp;quot;6/3&amp;quot; divides evenly, so we subtract 6 from 8, and get &amp;quot;2&amp;quot; as the remainder.&amp;lt;/code&amp;gt;&lt;br /&gt;
As said, it’s a great way to read if a number is a multiple of another. If a modulo expression returns “0”, that means it divided evenly, and is therefore a multiple of the number divided by.&lt;br /&gt;
&lt;br /&gt;
And that’s math!&lt;br /&gt;
&lt;br /&gt;
==== Comparison Operators ====&lt;br /&gt;
This sounds a lot more complicated than “math”, eh? It’s not that complicated – we’re comparing how two variables relate to one another, using expressions we’ve all seen before. The list of comparison operators are as follows:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt; — “less than”&lt;br /&gt;
* &amp;gt; — “greater than”&lt;br /&gt;
* &amp;lt;= — “less than or equal to”&lt;br /&gt;
* &amp;gt;= “greater than or equal to”&lt;br /&gt;
* == “equal to”&lt;br /&gt;
* ~= “not equal to”&lt;br /&gt;
&lt;br /&gt;
All of these expressions result in a boolean value, that is, it tells you if the result of what you're saying is true or false.&lt;br /&gt;
     &amp;lt;code&amp;gt;a = 10&lt;br /&gt;
     b = 7 + 3&lt;br /&gt;
     out(a == b) → true -- they ARE equal!&lt;br /&gt;
     out(a ~= b) → false -- they ARE NOT not equal (weird, I know)!&lt;br /&gt;
 &lt;br /&gt;
     c = 8&lt;br /&gt;
     d = 7&lt;br /&gt;
     &lt;br /&gt;
     out(c &amp;gt; d) → true -- 8 is greater than 7&lt;br /&gt;
     out(c &amp;lt; d) → false -- 8 is NOT less than 7&amp;lt;/code&amp;gt;&lt;br /&gt;
It’s important to note that Lua will consider different types to be different values. A string (ie a group of characters, like the word &amp;quot;hello&amp;quot; or the character sequence &amp;quot;15&amp;quot;) and a number will always be different. Lua will throw an error if you attempt to compare – using less-than or greater-than – any two separate types. Lua will not throw an error for “==” and “~=”, and if they are separate types, they will always be inequal.&lt;br /&gt;
     &amp;lt;code&amp;gt;a = 15&lt;br /&gt;
     b = &amp;quot;15&amp;quot;&lt;br /&gt;
     out(a == b) → false -- they are different types!&lt;br /&gt;
     out(a &amp;gt;= b) → error (attempt to compare number with string)&amp;lt;/code&amp;gt;&lt;br /&gt;
Do note that you can compare strings using greater or less than, but it compares the alphanumerical order of the first character. “0” is less than every number or letter, whereas “a” is greater than every number and less than every letter, and “z” is greater than every number or letter. Again, not really important, but you came here to read me ramble on about random stuff.&lt;br /&gt;
&lt;br /&gt;
And that ends our comparison operators!&lt;br /&gt;
&lt;br /&gt;
==== Logical Operators ====&lt;br /&gt;
Next up are our logical operators! We have three operators to concern ourselves with here: ''and'', ''or'', and ''not''.&lt;br /&gt;
&lt;br /&gt;
''Not'', as a logical operator, is pretty simple and super handy. It basically converts the value to the right of it into a boolean, and reverses it. Any value other than &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; will be converted into &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;; &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; will be converted to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;. Let’s take a look:&lt;br /&gt;
     &amp;lt;code&amp;gt;a = true&lt;br /&gt;
     b = nil&lt;br /&gt;
     out(not b) → true -- prints a double negative - not false, so true&lt;br /&gt;
     out(not a) → false -- reverses the value of &amp;quot;true&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
     c = 100&lt;br /&gt;
     out(c) → 100&lt;br /&gt;
     out(not not c) → true -- c is a number, &amp;quot;not c&amp;quot; would become &amp;quot;false&amp;quot;, and &amp;quot;not false&amp;quot; would become &amp;quot;true&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
The operators ''and'' and ''or'' are the hearts of a lot of logic in Lua. “If this and this, then do this”.&lt;br /&gt;
     &amp;lt;code&amp;gt;a = 15&lt;br /&gt;
     b = false&lt;br /&gt;
     if a == 15 and not b then -- &amp;quot;if a is equal to the number fifteen, and &amp;quot;not b&amp;quot; returns true, then ... &amp;quot;&lt;br /&gt;
         out(&amp;quot;It does the thing!&amp;quot;) -- &amp;quot; ... print the text! &amp;quot;&lt;br /&gt;
     end&amp;lt;/code&amp;gt;&lt;br /&gt;
You’ll practically always see these two used in this way, for ''conditional statements'' like that – if these conditions work out, then do this other thing.&lt;br /&gt;
&lt;br /&gt;
==== Homeless Operators ====&lt;br /&gt;
I don’t really have a category for these two operators, but they’re incredibly important.&lt;br /&gt;
&lt;br /&gt;
First off, we have concatenation, which we’ve seen before. It’s a way to add two strings of characters together, and make them into one happy family, using a simple pair of dots.&lt;br /&gt;
     &amp;lt;code&amp;gt;a = &amp;quot;Hello&amp;quot;&lt;br /&gt;
     b = &amp;quot;World!&amp;quot;&lt;br /&gt;
     out(a .. &amp;quot; &amp;quot; .. b) → Hello World!&amp;lt;/code&amp;gt;&lt;br /&gt;
A couple of notes real quick – I have the blank space in a string in the middle, to make sure it doesn’t print “HelloWorld!”. Also, I like to add spaces on either side of my concatenation operator, but that’s personal taste, this would also be valid:&lt;br /&gt;
     &amp;lt;code&amp;gt;out(a..&amp;quot; &amp;quot;..b) → Hello World!&amp;lt;/code&amp;gt;&lt;br /&gt;
And lastly, we have one more operator, and this time for the table type. The character &amp;lt;code&amp;gt;#&amp;lt;/code&amp;gt; can be used to read the number of entries within an array (NOTE: an array is a set of different elements under one variable name).&lt;br /&gt;
 It’s pretty awesome, let me show you:&lt;br /&gt;
     &amp;lt;code&amp;gt;table = {&amp;quot;this&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;an&amp;quot;, &amp;quot;example&amp;quot;, &amp;quot;array&amp;quot;}&lt;br /&gt;
     out(#table) → 5 -- there are five strings within the table!&amp;lt;/code&amp;gt;&lt;br /&gt;
Please note that you really shouldn’t use the &amp;lt;code&amp;gt;#&amp;lt;/code&amp;gt; operator if the table is not definitely an array. I’ll show you why using it on maps might mess something up:&lt;br /&gt;
     &amp;lt;code&amp;gt;table = {&lt;br /&gt;
         [1] = &amp;quot;test&amp;quot;,&lt;br /&gt;
         [2] = &amp;quot;example&amp;quot;,&lt;br /&gt;
         [5] = &amp;quot;boom&amp;quot;,&lt;br /&gt;
         [7000] = &amp;quot;woo&amp;quot;,&lt;br /&gt;
         [3] = &amp;quot;argh&amp;quot;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     out(#table) → 7000&amp;lt;/code&amp;gt;&lt;br /&gt;
In technicalities, it’s doing that because it’s reading the ''highest index in that table'', or the highest numerical key. Those are assigned automatically, in order, for arrays; in maps, there’s no such guarantee.&lt;br /&gt;
&lt;br /&gt;
==== Challenge 1 ====&lt;br /&gt;
As before, go to repl.it and mess with everything.&lt;br /&gt;
&lt;br /&gt;
Some suggestions on what to try:&lt;br /&gt;
&lt;br /&gt;
* Make an array out of various strings, concatenate them together using &amp;lt;code&amp;gt;table[1]&amp;lt;/code&amp;gt; and so on.&lt;br /&gt;
* Apply arithmetic to a series of 3 or more numbers&lt;br /&gt;
* Compare the length of two separate tables&lt;/div&gt;</summary>
		<author><name>MrIncongruous</name></author>
	</entry>
</feed>