Static Variables?

In my experience with with Lua, one thing particularly bugs me and requires me to expend a lot of effort trying to work my way around a simple bug. Here’s what I’ll do, for example


local tab = {]
tab.a = "Value"

local temp = tab

print(tab.a)

temp.a = nil

print(tab.a)



Would print:

“Value”
“nil”

Somehow, when you copy a table into a temporary one and then change the values from the temporary table, the changes get rerouted back to the original table. This brings me to the thread title, which is can you make static variables in Lua?

table.Copy is your friend :smile:

The reason the changes are getting rerouted back to the original table is because when you set a variable equal to an already existing table, it merely creates a handle for that table (another way to access it in code) instead of actually copying the table. So use table.Copy to actually create another copy of the table.

Thanks very much.