How to get reference to variable within a function?
Posted: Sat Jun 18, 2022 4:53 pm
Hi everyone.
So I have an interesting problem to solve.
I would like to modify a local variable inside a function
This is what I would like to do:
I prefer not to make the variable global and not to work with the actual variable name within the function. Instead I would like to work with just a reference to a variable so I can pass any variable to the function to work with.
I was searching the Lua documentations and the web but could not find any solution.
One thing that was repeatedly come up in searches is to use a C module to use pointers to get a reference to the variable in memory but no info was shared how to do that exactly and I don't have a lot of experience with C.
So I was wondering, is there anything like a pointer in Lua to pass a variable by reference to a function or could someone recommend a library to work with memory addresses of variables to get around this?
I would appreciate any help.
Thank you.
So I have an interesting problem to solve.
I would like to modify a local variable inside a function
This is what I would like to do:
Code: Select all
local a = 10
print(a) -->>output 10
myFunction = function(_value)
_value = _value + 1
end
myfunction(a)
print(a) -->> output 11
I was searching the Lua documentations and the web but could not find any solution.
One thing that was repeatedly come up in searches is to use a C module to use pointers to get a reference to the variable in memory but no info was shared how to do that exactly and I don't have a lot of experience with C.
So I was wondering, is there anything like a pointer in Lua to pass a variable by reference to a function or could someone recommend a library to work with memory addresses of variables to get around this?
I would appreciate any help.
Thank you.