1
votes

Why isn't this working? I'm trying to put all my object tables in a single table and use a forloop to iterate through each of them and draw. It shows an error message saying: "}" expected near "=" at line 5

function love.load()
solidstatic = {
ground = {x = 0,y = 160,width = 1000,height = 1000},
box = {x = 80,y = 100,width = 15,height = 15}
}

end


function love.draw()
for i,obj in ipairs(solidstatic) do
love.graphics.rectangle("fill",obj[x],obj[y],obj[width],obj[height])
end
end 

(edit) solved the error problem, I was running the wrong .lua file. But still, it doesn't draw anything on the screen

2
The solidstatic table has no array keys so ipairs doesn't iterate over anything. - Etan Reisner

2 Answers

1
votes

Two things. Firstly, you must use pairs instead of ipairs to list keys that are not numbers.

for i, v in pairs(table) do
    ...
end

You must also index the variables as a string.

t = {
    x = 1
}

t['x'] = 1
-- or
t.x = 1

This is because doing it without quotes would be indexing with the global variable x, which doesn't exist.

0
votes

You need to use pairs instead of ipairs to iterate over elements in solidstatic as there are no array keys in that table.