## table in table error

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
pauls313
Prole
Posts: 49
Joined: Tue Aug 02, 2016 3:07 am

### table in table error

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 a error message saying: "}" expected near "=" at line 5

Code: Select all

 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


zorg
Party member
Posts: 2801
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: table in table error

Simple, you forgot the comma (,) at the end of the line starting with ground =.
Since it found the word "box" instead of a comma, it expected the closing } bracket of the solidstatic table.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

pauls313
Prole
Posts: 49
Joined: Tue Aug 02, 2016 3:07 am

### Re: table in table error

Nop, still getting the same error message

I was messing with this for hours trying to solve it, and I eventually deleted the comma to see if it was the problem. I forgot to add it again, since I kept getting the same error message

pgimeno
Party member
Posts: 2027
Joined: Sun Oct 18, 2015 2:58 pm

### Re: table in table error

Works for me. Are you sure you've put a comma after the } like this?

Code: Select all

 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

The above code does not cause any errors.

pauls313
Prole
Posts: 49
Joined: Tue Aug 02, 2016 3:07 am

### Re: table in table error

Well, after hours messing with this I finally found the problem: I was editing the wrong code. Löve was running a code in another folder.

NEVERTHELESS, it doesn't do what it should. I just get a black screen

Tanner
Party member
Posts: 166
Joined: Tue Apr 10, 2012 1:51 am

### Re: table in table error

That's not how you get things out of tables in Lua.

Replace this.

Code: Select all

love.graphics.rectangle("fill",obj[x],obj[y],obj[width],obj[height])
With this.

Code: Select all

love.graphics.rectangle("fill",obj["x"],obj["y"],obj["width"],obj["height"])
Or this.

Code: Select all

love.graphics.rectangle("fill",obj.x,obj.y,obj.width.,obj.height)
You should read more able how tables work in Lua. It will save you a lot of time and frustation if you just read and understand some stuff. https://www.lua.org/pil/2.5.html

airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

### Re: table in table error

Tanner wrote:You should read more able how tables work in Lua. It will save you a lot of time and frustation if you just read and understand some stuff. https://www.lua.org/pil/2.5.html

pgimeno
Party member
Posts: 2027
Joined: Sun Oct 18, 2015 2:58 pm

### Re: table in table error

Also, you are using named items, for which ipairs is inadequate. Try pairs instead.

### Who is online

Users browsing this forum: RagingDave and 13 guests