Page 1 of 1

Tables And Metatables

Posted: Sat Jun 14, 2014 11:18 pm
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.

Re: Tables And Metatables

Posted: Sun Jun 15, 2014 9:14 am
by bartbes
You should start by telling us what you're trying to accomplish, and what you can't work out/get to work.

Re: Tables And Metatables

Posted: Sun Jun 15, 2014 2:06 pm
by DaedalusYoung
You don't need metatables to store graphics, just load them in a regular table and access them by the index number.

Re: Tables And Metatables

Posted: Sun Jun 15, 2014 9:03 pm
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

Re: Tables And Metatables

Posted: Sun Jun 15, 2014 9:12 pm
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.