Yes, you can use it as a key, but it will not mean what you think it means.
When key values are tables, they are looked up by reference. Thus, the following totally works:
local a = Vector2.new(5.9, 2.1)
local b = Vector2.new(1.3, 7.6)
local c = Vector2.new(1, 1)
local t = {}
t[a] = 5
t[b] = 8
t[c] = 9
print(t[a], t[b], t[c])
The problem is that just because two Vector2s have the same x and y does not mean they are they same object.
Imagine we weren't using Vector2s, but tables:
local a = {x = 1, y = 1}
local b = {x = 1, y = 1}
local t = {}
t[a] = 5
print(t[b])
Why do we get nil? Well, because a and b happen to contain the same information now, but later they could change:
b.x = 5
print(t[b])
a.x = 2
print(t[a])
Thus two objects are only truly equal if they change in exactly the same ways.
Lua doesn't know that Vector2s can't be changed, so it cannot apply that reasoning to map them to the same object.
Lua doesn't know, but you do!
An easy (though somewhat cumbersome) way to deal with this problem in general is to turn the objects into strings.
Two Vector2s with the same representation as a string represent the same point in space; and we don't have the problem we have with strings that we do with tables:
local a = Vector2.new(1, 1)
local b = Vector2.new(1, 1)
local t = {}
t[tostring(a)] = 5
print(t[tostring(b)])
You can use metamethods to make this process automatic for you:
local backing = {}
local t = setmetatable({}, {
__index = function(_, key)
return backing[tostring(key)]
end,
__newindex = function(_, key, value)
backing[tostring(key)] = value
end
})
local a = Vector2.new(1, 1)
local b = Vector2.new(1, 1)
t[a] = 5
print(t[b])
Multidimensional Arrays
If you're truly storing a grid of points, it may be useful to just use an array-of-arrays (commonly referred to as a "2D array"):
local grid = {}
for i = 1, 10 do
grid[i] = {}
for j = 1, 100 do
grid[i][j] = 0
end
end
print(grid[5][18])
grid[6][20] = 10
print(grid[6][20])
1 comment
Maybe consider using strings? print(mytable[vec2(1,0)]), where vec2() converts the given parameters into the format '%d,%d'.