Understanding scopes
In this section, we will learn what scopes are and why you have to keep them in mind when coding. First, we will start by looking at how a new scope is made. Scopes are something you have already worked with; you just did not realize it. Every time we had an if
statement, did you notice how the code before the end was spaced to the right? The reason for this spacing is because this section is a new scope. A quick note: spacing your code to the right does not create a new scope.
But what can we do with scopes? Scopes are not necessarily something that you can use but rather something you have to keep in mind. We have already seen a new scope being created when using an if
statement. There are a lot more situations where scopes are created. The following code is something you can do for the sole purpose of creating a scope:
do
print("New Scope")
end
When a scope starts, it executes the code within it. If there is another scope within an already existing scope, all of the data from the scope above is for the scope below. If there is data in a scope that the current scope is not in, you cannot access that data.
Scopes accessing certain data sounds really confusing. Here is an example that demonstrates it:
local outsideScopeData = "this is accessible everywhere"
do
-- New Scope
print(outsideScopeData)
-- New data in this scope
local insideScopeData = "This data is accessible in
this scope."
-- Printing in scope data
print(insideScopeData)
end
-- Printing data outside of the scope
print(outsideScopeData)
print(insideScopeData) – This data does not exist
Our first variable is not inside of a scope. Therefore, the scope it is in is called the global scope. This section of your code is accessible everywhere in your code.
Next, we start our first scope. To test whether we can access the variable from the global scope, we have a print
statement that prints the outsideScopeData
variable. We can access this variable because your new scope is inside the global scope. Because our new scope is inside the global scope, all of the data that the global scope has, our new scope has as well.
Then, we create a new variable inside of our new scope. This data does not exist in the global scope. Inside our new scope, we print this new data using the print()
function. We can do this because this data is inside the current scope and thus exists.
Our scope comes to an end. We can see this because of the end
line. After this, we are back in the global scope. First, we try to print outsideScopeData
. We can do this because this variable exists in the global scope. Then, we try to print a variable created inside a scope. However, printing this variable does not work. Printing this does not work because our current scope, the global scope, does not exist inside the scope where this variable has been made. Therefore, we cannot access the data from here.
What if we wanted to make the variable inside the scope and still print it inside the global scope? We can make a variable with no data inside the global scope. Then, we can set this empty variable to our desired value inside the new scope. This works because the variable is made in the global scope and set inside another scope. You can see this in practice in the following code snippet:
local outsideScopeData = "this is accessible everywhere"
local dataSetInScope
do
-- New Scope
print(outsideScopeData)
-- Setting data in this scope
dataSetInScope = "This data is accessible in this
scope."
-- Printing in scope data
print(dataSetInScope)
end
-- Printing data outside of the scope
print(outsideScopeData)
print(dataSetInScope)
As you can see, we have created a variable missing the = your_data
part of a variable. If we do this, Luau automatically turns this into a nil
variable. We could have done this ourselves as well. Our variable would have looked like this:
local dataSetInScope = nil
Then, inside the scope, we set the variable with new data. Because the variable exists in the global scope, the data is not thrown away when the scope ends. This is why we can print the correct result in the last line of this script.
Now that we know how scopes work, we have finished everything that the first chapter offers. In the following section, we put into practice the learned information.