placing anim8 parameters inside a table

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
Wanda Siren
Prole
Posts: 2
Joined: Tue Apr 30, 2019 4:41 pm

placing anim8 parameters inside a table

Post 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
User avatar
miniaturedog
Prole
Posts: 11
Joined: Sat Apr 27, 2019 8:21 pm

Re: placing anim8 parameters inside a table

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

Re: placing anim8 parameters inside a table

Post by pgimeno »

Also, remove 'local'.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: placing anim8 parameters inside a table

Post 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)
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.
Wanda Siren
Prole
Posts: 2
Joined: Tue Apr 30, 2019 4:41 pm

Re: placing anim8 parameters inside a table

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 3 guests