<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://tw-modding.com/index.php?action=history&amp;feed=atom&amp;title=Lua%3AScopes_%26_Stuff</id>
	<title>Lua:Scopes &amp; Stuff - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://tw-modding.com/index.php?action=history&amp;feed=atom&amp;title=Lua%3AScopes_%26_Stuff"/>
	<link rel="alternate" type="text/html" href="https://tw-modding.com/index.php?title=Lua:Scopes_%26_Stuff&amp;action=history"/>
	<updated>2026-05-14T04:13:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://tw-modding.com/index.php?title=Lua:Scopes_%26_Stuff&amp;diff=1214&amp;oldid=prev</id>
		<title>Grimmys: Add LUA tutorial category</title>
		<link rel="alternate" type="text/html" href="https://tw-modding.com/index.php?title=Lua:Scopes_%26_Stuff&amp;diff=1214&amp;oldid=prev"/>
		<updated>2024-03-11T19:25:43Z</updated>

		<summary type="html">&lt;p&gt;Add LUA tutorial category&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 15:25, 11 March 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l46&quot;&gt;Line 46:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 46:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;As you can see with the example_thing() function above, proper indentation is a GREAT way to visibly see scopes and quickly tell where a variable is and isn’t available.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;As you can see with the example_thing() function above, proper indentation is a GREAT way to visibly see scopes and quickly tell where a variable is and isn’t available.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Lua Tutorials]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Grimmys</name></author>
	</entry>
	<entry>
		<id>https://tw-modding.com/index.php?title=Lua:Scopes_%26_Stuff&amp;diff=147&amp;oldid=prev</id>
		<title>Vandy: Created page with &quot;A super important concept in Lua is the concept of ''scope''. Every single variable made has a ''scope'', which is used to define where a variable can be accessed.  By default...&quot;</title>
		<link rel="alternate" type="text/html" href="https://tw-modding.com/index.php?title=Lua:Scopes_%26_Stuff&amp;diff=147&amp;oldid=prev"/>
		<updated>2021-02-07T18:40:19Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;A super important concept in Lua is the concept of &amp;#039;&amp;#039;scope&amp;#039;&amp;#039;. Every single variable made has a &amp;#039;&amp;#039;scope&amp;#039;&amp;#039;, which is used to define where a variable can be accessed.  By default...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;A super important concept in Lua is the concept of ''scope''. Every single variable made has a ''scope'', which is used to define where a variable can be accessed.&lt;br /&gt;
&lt;br /&gt;
By default, all variables are &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;, which means they’re in the entire global environment. The global environment is a slightly-fancy way of saying “available within the entire program”. When writing Total War scripts, it’s a little suboptimal to use global variables, for a few reasons – 1) What if someone else wants to use a variable named &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;, and you do as well? This would force you to have a fully unique name for every single variable everywhere. Kinda a pain! 2) Saving that variable globally is taking up memory, since that variable will never be destroyed. It will take a lot of work to actually cause performance issues, but it’s good to be mindful of. 3) What if we want our variable to be a secret? Huh? They can’t get our birthday present! ''gollum''&lt;br /&gt;
&lt;br /&gt;
And all this is where the keyword &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; comes in. The &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; keyword defines a specific scope for a variable, and prevents them from being a global variable.&lt;br /&gt;
 &amp;lt;code&amp;gt;function example()&lt;br /&gt;
     local var = &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
     out(var)&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 example() → Hello World!&lt;br /&gt;
 out(var) → nil -- the variable &amp;quot;var&amp;quot; doesn't exist here!&amp;lt;/code&amp;gt;&lt;br /&gt;
In this example, the variable &amp;lt;code&amp;gt;var&amp;lt;/code&amp;gt; is defined within the function &amp;lt;code&amp;gt;example&amp;lt;/code&amp;gt;‘s definition. That means that &amp;lt;code&amp;gt;var&amp;lt;/code&amp;gt; is only available from ''where it is defined'' (ie. local var =) to ''where the scope ends''. In this case, the scope ''ends'' at the &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
This is as good a time as any to look at the other ways scope can be defined. A variable can be scoped to the ''root'' of a script, or any number of nestled scopes within that root.&lt;br /&gt;
&lt;br /&gt;
Other key-words other than &amp;lt;code&amp;gt;function&amp;lt;/code&amp;gt; that define a scope (and their end point) are as follows:&lt;br /&gt;
&lt;br /&gt;
* do –&amp;lt;nowiki&amp;gt;[[ the scope ]]&amp;lt;/nowiki&amp;gt; end&lt;br /&gt;
* function function_name() –&amp;lt;nowiki&amp;gt;[[ the scope ]]&amp;lt;/nowiki&amp;gt; end&lt;br /&gt;
* if –&amp;lt;nowiki&amp;gt;[[ whatever ]]&amp;lt;/nowiki&amp;gt; then –&amp;lt;nowiki&amp;gt;[[ the scope ]]&amp;lt;/nowiki&amp;gt; end&lt;br /&gt;
* while –&amp;lt;nowiki&amp;gt;[[ whatever ]]&amp;lt;/nowiki&amp;gt; then –&amp;lt;nowiki&amp;gt;[[ the scope ]]&amp;lt;/nowiki&amp;gt; end&lt;br /&gt;
&lt;br /&gt;
Let’s take a slightly-complicated look at that:&lt;br /&gt;
 &amp;lt;code&amp;gt;local a = 5 -- visible to everything BELOW this line!&lt;br /&gt;
 &lt;br /&gt;
 function example_thing()&lt;br /&gt;
     local b = a -- visible to everything BELOW this line, until the &amp;quot;end&amp;quot; that matches &amp;quot;function&amp;quot;&lt;br /&gt;
     do &lt;br /&gt;
         local c = 15 -- visible to everything BELOW this line, until the &amp;quot;end&amp;quot; that matches &amp;quot;do&amp;quot;&lt;br /&gt;
         out(c) → 15&lt;br /&gt;
         out(b) → 5&lt;br /&gt;
     end&lt;br /&gt;
     out(b) → 5&lt;br /&gt;
     -- c is NOT available here!&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 -- b and c are NOT available here!&lt;br /&gt;
 out(a) → 5&amp;lt;/code&amp;gt;&lt;br /&gt;
This is a good point as any to explain two things, and issue two directives:&lt;br /&gt;
&lt;br /&gt;
# ALWAYS use the keyword &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt;.&lt;br /&gt;
# ALWAYS use good indentations! Scopes should be easily detected.&lt;br /&gt;
&lt;br /&gt;
Be smart with where exactly the term “local” is used – you’ll pick up how later on as you go and pick my brain, but for now, understand that you must ALWAYS put the keyword &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; before a variable!&lt;br /&gt;
&lt;br /&gt;
As you can see with the example_thing() function above, proper indentation is a GREAT way to visibly see scopes and quickly tell where a variable is and isn’t available.&lt;/div&gt;</summary>
		<author><name>Vandy</name></author>
	</entry>
</feed>