Skip to main content
Version: 3.14.x.x LTS

Variable scopes

All variables, no matter where they are defined, are accessible on a per-request basis. Changes of such variables are only visible for the request which did the change.

You can have local variables in functions:

function something(some, parameters)
local var1 = 10
doSomthing(var1)
end

The scope of such local variables is inside the body the variables are defined. In Lua you need to define it local. Else the variable is accessible globally with in a request.

You can have variables outside of functions and hook functions, let's call them global variables. The value of those variables is set on load. On every request, they will be reset to the value they had on load.

myConstVariable = "foo"
function myHook(request, response, chunk)
doSomething(myConstVariable)
end

Furthermore, you can call functions on load which calculate global variables, which will be reset to that calculated value on each request.

function getFirstLine(parameter)
-- get somehow the first line
end
function calculateMyParameter()
myGlobalCaluclatedConst = getFirstLine(MyParameter)
end
calculateMyParameter()

Here we assume that we defined our Script.Namespace to My and we also defined the MyParameter with some values. The myGlobalCalculatedConst will be reset on every request to the value the calculateMyParameter() calculated on start. On start the script is interpreted from top down and you can see in the sample that we call calculateMyParameter()in the end.