Scope

From Legacy Roblox Wiki
(Redirected from Environment)
Jump to navigationJump to search

What is Scope?

Scope is a block of code in which variables are declared. for loops, while loops, repeat until loops, functions, if then, and do end chunks all create a new variable scope. For most of the following examples, the do end chunk will be used as a simple way of creating a variable.

Variables can be declared in two ways: local (with the local keyword), and global. Global variables are always in scope: no matter where you declare them, they are always accessible.

Example
function myFunction()
	myVariable = 64 --globally scoped
end

myFunction()

print("myVariable is " .. myVariable)

Output:
myVariable is 64


When myFunction was called, myVariable was set to 64. Since this variable is globally scoped, it could be accessed outside the function. This is why the output was "myVariable is 64". Take a look at this example using the local keyword to change the scope of a variable.

function myFunction()
	local myVariable = 64
end

myFunction()

print("myVariable is " .. myVariable) -->  myVariable is nil

"myVariable is nil" was outputted because myVariable was confined to myFunction's environment or its scope. Therefore, it was not able to access the value outside of that function.

Global variables can be set easily by removing the word local, but for even better control over a variables scope, you can use setfenv.

Example
function myFunction()
	function myFunction2()
		print(a)
	end
	myFunction2()
	print(a)
end
setfenv(myFunction, {a = 64,print=print})
myFunction()
print(a)

--[[
Output:
64
64
nil
]]


This will output two values of 64, and will output one nil. The variable's environment was set to the function 'myFunction', and therefore the variable was also accessible by descendant environments (myFunction2). The setfenv function was called taking in two arguments, the environment in which the variable was to be placed in, and a table with the variables to be set. The variable 'a' was set, along with the reset for the global table.

Lexical Scoping

In the last example you may have noticed that I said "the variable was also accessible by descendant environments". This concept is called lexical scoping.

Example
if true then --level 1
	local a = 82.5
	if true then --level 2
		local b = 64
	end
end


This is a simple example with two conditionals. If you recall, conditionals also have their own environment. Here is a snippet to prove this:

Example
if true then --level 1
	local a = 82.5
	if true then --level 2
		local b = 64
	end
	print(b) --> nil
end


Its nil because variable b was confined to the scope of the second conditional. Lexical scoping will allow one to do this:

Example
if true then --level 1
	local a = 82.5
	if true then --level 2
		print(a) --> 82.5
		local b = 64
	end
end


Even though the second conditional has its own environment, because of lexical scoping, a was inherited. Variables that are lexically scoped are called upvalues.

See Also