Kend2000
Kend2000

Reputation: 43

Lua, table converted to a number?

I am simply adding numbers together but it continues to error. I used type() to check if vector is a table or not and it always said it was but it continues to say that it is a number.

Can anyone tell me why this is happening and a way to fix it(the variable vector is a vector3 object)? Any help is greatly appreciated.

Vector3:

function new(x, y, z)
  return setmetatable({x = x, y = y, z = z}, meta) --{} has public variables
end

All of the Vector3 file here: http://pastebin.com/csBmJG36

ERROR:

attempt to index local 'vector' (a number value)

SCRIPT:

function translate(object, x, y, z)
    for i, v in pairs(object) do
        if (i == "Vertices") then
            for _, q in pairs(v) do
                for l, vector in pairs(q) do
                    vector.x = vector.x + x;
                    vector.y = vector.y + y;
                    vector.z = vector.z + z;
                end
            end
        end
    end
end

Upvotes: 1

Views: 703

Answers (1)

Deduplicator
Deduplicator

Reputation: 45694

Let's refactor your code by removing the loop-switch anti-pattern:

function translate(object, x, y, z)
    for _, q in pairs(object.Vertices) do
        for l, vector in pairs(q) do
            -- Test the type of vector here...
            vector.x = vector.x + x;
            vector.y = vector.y + y;
            vector.z = vector.z + z;
        end
    end
end

So, the error occurs with an access to object.Vertices[_][l].x.

That would be a curious vertex-list which contains lists of vertex-lists instead.

Upvotes: 2

Related Questions