Page 1 of 1

placing anim8 parameters inside a table

Posted: Tue Apr 30, 2019 6:01 pm
by Wanda Siren
hello, i was testing around the engine since i'm a beginner, but i got an error:
Syntax error: main.lua:6: '}' expected (to close '{' at line 4) near 'local'
with this code:

Code: Select all

local anim8 = require "anim8"

function love.load()
  player {
    img = love.graphics.newImage("sheet.png")
    local g = anim8.newGrid(72, 117, img:getWidth(), img:getHeight())
    anim = anim8.newAnimation(g('1-7',1), 0.1)
  }
end

function love.update(dt)
  player:update(dt)
end

function love.draw()
  player:draw(img, 100, 100)
end
using the anim8 library

basically, i wanted to put all the animation parameters inside a player table but it keeps giving me errors and i don't understand what i'm doing wrong

any help is appreciated

Re: placing anim8 parameters inside a table

Posted: Tue Apr 30, 2019 10:22 pm
by miniaturedog
Your problem is with your table itself. You need to have an equals sign so that player { becomes player = {.
Also, items within a table have commas, except for the last line, like so:

Code: Select all

table = {
    item 1,
    item 2,
    item 3
}
So your fix should be:

Code: Select all

function love.load()
  player = {
    img = love.graphics.newImage("sheet.png"),
    local g = anim8.newGrid(72, 117, img:getWidth(), img:getHeight()),
    anim = anim8.newAnimation(g('1-7',1), 0.1)
  }
end
(Apologies if the tabs aren't formatted correctly; I'm on mobile.)

Re: placing anim8 parameters inside a table

Posted: Tue Apr 30, 2019 10:23 pm
by pgimeno
Also, remove 'local'.

Re: placing anim8 parameters inside a table

Posted: Tue Apr 30, 2019 10:52 pm
by zorg
Then again, if you don't need g elsewhere, then you can do two things:

Code: Select all

function love.load()
  player = {}
  player.img = love.graphics.newImage("sheet.png")
  local g = anim8.newGrid(72, 117, player.img:getWidth(), player.img:getHeight())
  player.anim = anim8.newAnimation(g('1-7',1), 0.1)
end
or

Code: Select all

function love.load()
  player = {}
  player.img = love.graphics.newImage("sheet.png")
  player.anim = anim8.newAnimation(anim8.newGrid(72, 117, player.img:getWidth(), player.img:getHeight())('1-7',1), 0.1)
end
I also separated the member definitions out from the table creation, since those img:getWidth and getHeight calls would error if you tried to do it that way. (for two reasons, one, the final variable would be called player.img, not just img, and second, you can't reference stuff in a table constructor like that, if you're also defining it in there at the same time, even if it's an earlier item)

Re: placing anim8 parameters inside a table

Posted: Wed May 01, 2019 8:41 am
by Wanda Siren
hello again, i followed everyone advice and i finally managed it to work!

Code: Select all

local anim8 = require "anim8"

function love.load()
  player = {}
    player.img = love.graphics.newImage("sheet.png")
    player.anim = anim8.newAnimation(anim8.newGrid(72, 117, player.img:getWidth(), player.img:getHeight())('1-7',1), 0.1)

end

function love.update(dt)
  player.anim:update(dt)
end

function love.draw()
  player.anim:draw(player.img, 100, 100)
end
for future reference, my code looks like this, since the parameters inside the parenthesis gave me an error like zorg said

thank you again for the help :D