1

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

1
  • The solidstatic table has no array keys so ipairs doesn't iterate over anything. Commented Aug 4, 2016 at 2:16

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.