Is it possible to get a variable's memory address?

Anyway I can do something like this? Basically I want to pass a variable’s memory address as a function argument instead of passing the variable itself.

local myVariable = 10
randomFunction(&myVariable)
function randomFunction(aMemoryAddress)
  print(aMemoryAddress)
end

You cannot directly pass the in-memory address of a variable like you would in languages such as C or C++. Lua abstracts this all out to keep things simple and safe, however you can do this with tables.

1 Like

Tables were my first though too, just thought there could be an easier way. Thanks anyways.

1 Like

If you need to pass something by reference, only tables and fat userdata are done that way. Instead of wrapping a single value in a table, you should use upvalues.

local Value = 1

local function UpdateValue(NewValue: number)
	Value = NewValue
end

Closures are very fast to create in Luau, but just don’t use them unless needed (signal callbacks can be static functions instead of lambdas, for example).