Help Help please (scoping)
What is scoping and is it really essential to know when you script?
I really can’t understand it so I’m planning to learn it when I understand more things
Or do I have to understand it before I get into more stuff?
(edit: Thank you all for replying!! Really helped alot!)
7
Upvotes
3
u/Calaverd Nov 14 '25
Scoping is one of those concepts that seems a bit weird at first, but it's actually pretty simple once you get it. I like to think of scope like rooms in a house.
Basically, whenever you create a function, a loop, or an if statement, you close it with an "end". Everything between the beginning and that "end" is like a room with walls.
The rule is that variables that you define with
localinside a room only exist in that room and in any smaller rooms you create inside it. They can't be seen from outside.So inner rooms can see variables from outer rooms, but outer rooms can't see variables from inner ones. Variables flow inward, never outward.
2
u/NabePup Nov 14 '25 edited Nov 19 '25
Scope is a an important concept that a lot, nearly every language, has in some form or another. There’s a lot of good explanations here already, but I’ll paste below an answer of mine from another question/thread. Hopefully it’s not too wordy or lengthy! (it's also best viewed on desktop and not mobile)
———————————————————————————
Most programming languages have the concept of “scope”. It’s really a pretty straightforward concept, but isn't always the easiest to explain and sometimes takes a little practice and time to fully understand it. Essentially, it’s the "boundary" of where something is accessible. If you have a lua module (which is really just a lua file):
The
var’s scope is that entire module (file). It’s accessible anywhere in that file, such as in the function. The functionfuncalso has a scope of that module just likevardoes and can be called anywhere in that module.However, if that variable were declared with local inside the function then its scope would be that function and it would not be accessible outside that function:
A function can return something so it’s accessible outside its scope where it was called. So you could do:
Lua is kinda “special”…as in weird/unconventional that if you declare something like a variable or function without the local keyword then it’s in the “global scope” or sometimes simply referred to as global which means it’s accessible everywhere. Generally global variables are a bad practice and should be avoided. So doing:
would work since var was declared without the
localkeyword giving it global scope thus making it accessible outside of the function, generally though you wouldn’t want to do this.Note that
returndoesn’t “expand” or "increase" the scope of anything. It makes it so that a value is returned/exposed so that it then can be accessed where the function is called at its call site.A function can also accept or consume arguments. This kind of does the inverse of return. Where return exposes a value so that it can be accessed outside of the function's scope where it's called, an argument makes it possible so that a function can be passed a value that's outside of its scope so it's then accessible within the function's scope. For instance:
As the examples above show, there's usually a way to make it possible to "share" or pass things between scopes. For instance a function that can be passed arguments and/or returns a value. You can also return something from a module so that way when that module gets imported into a different module it will be accessible in that module's scope. For example, let's say you have 2 modules (remember, these are really just 2 separate lua files):
and then in a separate module:
I hope this helps and reduces confusion and not increase it! Also note, naming a module something super generic like "module_a" or "module_b" is not a good practice.
2
u/kilkil Nov 14 '25 edited Nov 14 '25
"scoping" is a concept in programming that refers to restricting where variables can be accessed. In programming, we say that some variables have a limited scope, or equivalently that they are scoped to such-and-such region of code. If a variable has a defined scope, it means that it cannot be accessed from outside that scope.
this is important to understand for scripting, because the question of where you declare a variable usually determines what its scope will be.
Here is how scoping works in Lua:
https://www.lua.org/pil/4.2.html
in a nutshell, here is my understanding:
Lua, like many other languages, has block scoping. That means when you create a variable within a block, it will it will only be available to other code within that block. A block is usually things like:
Example:
In the above code,
print(foo)will printnil, because the variablefoois only available inside the if-statement.Here is another example:
In the above code,
print(foo)will print"bar", because the variablefoois available to all code inside its block – including other blocks inside of it.3
u/MildlySpastic Nov 14 '25
I am not familiar with Lua, but if scoping in Lua is anything like in any other programming language, is basically where variables and functions can be accessed.
In the Global scope, variables and functions can be accessed from anywhere, making them very useful to use on the go but vulnerable to overwritting and security issues.
On the other hand, being in the local scope means that variables/functions can only be accessed inside the functions/components they were defined, making them more secure and specific for that context, but less reusable.
3
u/Denneisk Nov 14 '25
Okay but now to ruin your understanding, let's jump to the really cool stuff where you have what Lua calls "upvalues", others call "closures"
2
u/EvilBadMadRetarded Nov 14 '25
Lua Visibility Rules5.1 / 5.3The
scopeof a local variablebegin afterthe declaration andend beforethe end of the enclosing block (the innermost block that includes the declaration). So each local variable has its own scope.Local variables can be declared explicitly with keyword 'local' or implicitly in some construct, eg. for statement, function parameters definition.
related:
block/chunk5.1 / 5.3goto & label statement5.3.