Tables And Metatables

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Tables And Metatables

Post by xFade »

Code: Select all


local gs = {}
gs.__index = gs

local i = ""

function gs:Init(x)
  i = x
  print(x.." Initialized!")
end

setmetatable(gs, {
    _call = function(cls,...)
      return cls.new(...)
    end,
})

function gs.NewGif(init)
  local self = setmetatable({},gs)
  local frames = love.filesystem.getDirectoryItems(init)
  print("Loading Frames From..."..init)
  for i = 1, #frames do
   -- Help Here!
  end
  return self
end

return gs
Okay so I'm not very familiar with tables/meta-tables in lua, but I do know a thing or two. What I'm trying to accomplish is to create a new image and store it in a table. And to be able to access it to draw it later on. Help is much appreciated and needed.
(ง'̀-'́)ง
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Tables And Metatables

Post by bartbes »

You should start by telling us what you're trying to accomplish, and what you can't work out/get to work.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Tables And Metatables

Post by DaedalusYoung »

You don't need metatables to store graphics, just load them in a regular table and access them by the index number.
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Tables And Metatables

Post by xFade »

DaedalusYoung wrote:You don't need metatables to store graphics, just load them in a regular table and access them by the index number.
Yes but how do I do it without having to write it out. In a form of a loop. How can I store all the images in the table with a loop? Like so...

Code: Select all

local frames = love.filesystem.getDirectoryItems(init)
  print("Loading Frames From..."..init)
  for i = 1, #frames do
    print("Frame: "..i.." "..frames[i])
    -- Store Image in table :/
  end
(ง'̀-'́)ง
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Tables And Metatables

Post by DaedalusYoung »

Something like this:

Code: Select all

local images = {}
local frames = love.filesystem.getDirectoryItems(init)
  print("Loading Frames From..."..init)
  for i = 1, #frames do
    print("Frame: "..i.." "..frames[i])
    -- Store Image in table
    images[i] = love.graphics.newImage(frames[i])
  end
Probably not entirely complete though, you'll need to remove directories from the frames table first. And then assuming all the other files are indeed image files.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 2 guests