table in table error

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
pauls313
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

table in table error

Post by pauls313 »

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 
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: table in table error

Post by zorg »

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 :3True 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
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

Re: table in table error

Post by pauls313 »

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
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: table in table error

Post by pgimeno »

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
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

Re: table in table error

Post by pauls313 »

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
User avatar
Tanner
Party member
Posts: 166
Joined: Tue Apr 10, 2012 1:51 am

Re: table in table error

Post by Tanner »

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
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: table in table error

Post by airstruck »

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
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: table in table error

Post by pgimeno »

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

Who is online

Users browsing this forum: No registered users and 84 guests