Looks like I can’t figure out what’s wrong with that table. I’ve checked the forum, and found nothing helpful… seems like what I’m stumbled on is obvious to others.
I am generating a new table item on top of the table, and then I try to add something to that new item. But it seems like changes are being made to it’s first position.
local atable =
{
{ x = 0, y = 0}
}
table.insert(atable, atable[1])
atable[table.maxn(atable)].x = 1
atable[table.maxn(atable)].y = 1
But in that case atable[1].x = 1 while it should be 0
You need to create some table-copy-function before you run table.insert with the new element. There are no built in function for this, since behaviour depends on what you actually want to do (for example, do you want sub tables to also be copied by value, aka deep copy, or should they be copied by reference?).
Also, worth noting, from the Lua reference regarding table.maxn:
To do its job this function does a linear traversal of the whole table.
Which might be a performance issue depending on how big your table is. In your example you are actually traversing the table twice to get the same number. I’m not 100% sure but I think #atable should just return the size of the table (if it’s a regular “array”), but also store it in a temp variable so you don’t have to do it twice;
Thanks! Well… not sure how to create one without that table.insert thing. I’ll check that.
Actually, table.insert works perfectly in my code in some other function. Where whole table is exporting to a file, and then loading to a new table. Couldn’t think there will be a problem with that.
If you are just working with positions, you can use vectors. The vmath.vector3() function will copy a vector if you give it another vector as the argument.
local pos1 = vmath.vector3(0, 0, 0)
local pos2 = vmath.vector3(pos1) -- `pos2` is now a different vector than pos1, but has the same coordinates.
pos2.x, pos2.y = 1, 1
If you need to create a copy of a previous entry I would create a simple copy-function for this, here are some implementation alternatives: http://lua-users.org/wiki/CopyTable
In some cases it might make more sense to have a function to create a new “entry” instead, maybe passing in a previous entry and just copy some of the fields… I don’t know your exact use case, but for example one might do something like this;
function new_entry(x, y)
local entry = {x = x,
y = y,
some_random_value = math.random(),
some_static_field = 42
}
return entry
end
...
-- and usage;
table.insert(atable, new_entry(12, 45))
-- or using x and y from a previous entry;
local prev_entry = atable[#atable]
table.insert(atable, new_entry(prev_entry.x, prev_entry.y))