Skip to content

Backend of a script

The script itself is just a very big function. When you load a script, the text inside basically goes into a body of temporary function and that function is being called by the lua engine. This results into the execution of the script.

Each script can have an unlimited amount of functions, variables and tables, the only limitation is your RAM and CPU. The more you do, the slower it gets.

Let's take this short script as an example:

1
2
3
4
5
6
7
local my_var = 0

function calculate_delta(a, b)
    return a - b
end

print( calculate_delta(my_var, 5) )

When you load such a script, it will be turned into a function and executed like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
function temp_script()
    local my_var = 0

    function calculate_delta(a, b)
        return a - b
    end

    print( calculate_delta(my_var, 5) )
end

temp_script()

However, some engine implementations don't restrict globals into only one script scope and this could result into weird consequences.