Yes you are right, but uses for that can be different even tho i don't use them too, you can use them as a decoration like:
function()
do
assert()
assert()
assert()
assert()
assert()
assert()
if not x then
return
end
end
end)
Even tho you do not have to, also another use is for example having classes in one script and you would need to limit amount of variables so you do
do
local class1 = {}
function
class1.test = true
end
do
local class2 = {}
function
class1.test = true
end
print(class1)
print(class2)
And here you prevented uneccessary variables, overall it's mostly useless but i have found it a use once, for example i could split the code into modules but i needed one variable to be accessed from all the classes and defining it in every single module would be ehh so i used do for that:
local ImportantVariable = "WideSteal"
do
local class1 = {}
ImportantVariable.LoadClass(class1)
end
do
local class2 = {}
ImportantVariable.LoadClass(class2)
end
Instead of doing this in every single module
local ImportantVariable = "WideSteal"
local ImportantVariable = "WideSteal"
BUT here setfenv comes instead, if every single module will have function Init then you can get rid of do...end
local CustomEnviroment = {
ImportantVariable = "WideSteal",
print = print,
}
for module in pairs(modules) do
module.init(CustomEnviroment)
end
function module1.init(CustomEnviroment)
setfenv(1, CustomEnviroment)
print(ImportantVariable)
end)
function module1.init(CustomEnviroment)
setfenv(1, CustomEnviroment)
print(ImportantVariable)
end)
so basically do...end has probably no uses except for decoration
1 comment
yeah I can't say I've ever used get/setfenv either. it's not like they dont have their uses obviously, but I clearly haven't encountered a scenario in which they would be necessary. i guess thats the situation I'm in with do-end blocks