Lua: how do I point to an array item through a variable?

Tags: pointers lua
By : jv110
Source: Stackoverflow.com
Question!

For example:

local array = {2, 6}
local value = array[1]

...
value = 4 -- Intended: change the value in the original array
...

print(array[1]) -- Will print 2, not 4 as intended

Is this possible? I ask because I'm working on an graphics API and I want to be able to do color = something instead of frag.data[1] = something on the shaders, it looks a lot better and it's easier to use and understand.

By : jv110


Answers

It's impossible. Lua number type is value, not pointer, but you can do something like array = {{2}, {6}}, then

local array = {{2}, {6}}
local value = array[1]
value[1] = 4
print(value[1])
By : val


This video can help you solving your question :)
By: admin