Scope refers to the portion of the document structure where a particular piece of code can see other elements, or be seen by other elements. The general scoping rule for scripts is that the closer an element is to a top-level section, the more it can be seen other elements. When elements are declared deep inside of nested sections they are harder to see.
In the following example code and representative diagram, Parent A is a top-level section that contains four children: two buttons, a stack, and a section. Let's use the diagram to define who can see who:
- Children 1, 2, 3 & 4 can see each other
- Children 1 & 2 cannot see the children inside Children 3 & 4 (i.e. Children 3a, 3b, 3c & 4a)
- Children 3a, 3b & 3c can see each other
- Children 3a, 3b & 3c can can also see Children 1, 2, 3 & 4
- Child 4a can see Children 1, 2, 3 & 4
<sectionNoTitle name="parentA"> <button name="child1" /> <button name="child2" /> <stack name="child3"> <button name="child3a" /> <button name="child3b" /> <button name="child3c" /> </stack> <sectionNoTitle name="child4"> <button name="child4a" /> </sectionNoTitle> </sectionNoTitle>
If Child 1 contains a script to control Child 3a it will first need to specify where to look for it, i.e. inside Child 3.
<button name="child1"> <onClick> child3.child3a.Text = "New Text on Button" </onClick> </button>
<button name="child1"> <onClick> child3a.Text = "New Text on Button" </onClick> </button>
This script would not work
This script works
For variables that are declared in Lua, if you specify that the variable is local then it's scope is limited to elements in the same level or higher, as outlined above. If you declare a variable without the local modifier, then it is treated as global and is open to the entire document. To avoid issues with shared names, or other conflicts, we recommend that all variables be declared as local unless absolutely necessary.