Using table.insert() on properties of objects

I’m sure it’s something rather simple, but so far, I haven’t been able to find out what makes stuff don’t work, so I decided to ask. (I’m new with Lua)

my code in the setup section:


And it prints 0.

So, any help is appreciated, but may I ask you to try out your suggested fix prior posting? Thank you in advance.

A mesh is a special object and its vertices aren’t really a lua table. When you do body.vertices = {table of vertices} then there’s a bit of magic going on underneath that does the actual assignment. You can emulate this by using the vertex method to set a vertex. To do this, you need to keep track of the number of vertices in your mesh and you need to periodically resize the mesh.

(Incidentally, make sure you insert vertices in threes)

My generic addQuad method does this:

function addQuad(t)
    local m = t.mesh
    local n = t.position or 0
    if n > m.size - 12 then
        m:resize(n + 300)
    local v = t.vertices
    for k,l in ipairs({1,2,3,2,3,4}) do
        if v[l][2] then
        if v[l][3] then
    return n + 6

The input to the routine is a table which specifies a mesh and a table of four vertices. These vertices are then added to the mesh with the effect of adding a quadrilateral with those vertices as the corners. They are optionally coloured.

The important parts (for this discussion) are as follows. First, we check if we’ve been given a position. If so, we add the quadrilateral at that position possibly overwriting already-set vertices. If the position is beyond the end of the mesh we resize the mesh so that it is big enough (we’re generous so we don’t have to do this too often). Then we enter a loop which calls the vertex method of the mesh. This sets the vertices. If their position is beyond the end of the mesh, they are added to its internal vertex table. Otherwise they replace those already there. The return value of the function indicates the position to continue adding at if you want to add something straight after what we just did.

Simply put, the .vertices is ‘magic’, it isn’t the same as a lua table. You can construct your own table and then assign the body.vertices, but keep in mind that your table has to be properly filled with vec3’s or else the magic won’t work.

Been looking for an opportunity to link to this quote for some time: (don’t worry, it’s clean).

Well, thank you very much for your help, my code is all working now.