Page 1 of 1

How to work with multiple pictures and variables name?

Posted: Tue Nov 02, 2021 5:03 pm
by Kitsune64
Hi,

How to work with multiple pictures without copy paste a code?

example:

picture1=love.graphics.newImage( filename1, settings )
picture2=love.graphics.newImage( filename2, settings )
picture3=love.graphics.newImage( filename3, settings )


In my code I want to call picture"Number1" and after picture"Number2" ... Without to create a code like this:
if picture1 then
else if picture2 then...


----Reedit----------------------

I have find a way by putting these pictures in a array but I want to free memory usage for not have double picture loaded. If you have the way to unload picture this is welcome.

Thanks

Re: How to work with multiple pictures and variables name?

Posted: Tue Nov 02, 2021 5:43 pm
by darkfrei

Code: Select all

local filenames = {"abc.png", "def.png"} -- list of file names
local settings = {} -- your settings
local pictures = {} -- empty table
for i, filename in ipairs (filenames ) do -- iterator
	local picture = love.graphics.newImage( filename, settings ) -- new picture
	teble.insert (pictures, picture) -- insertion new picture to the pictures table
end
Also you can store pictures in global https://www.lua.org/pil/14.2.html:

Code: Select all

_G[filename] = picture

Re: How to work with multiple pictures and variables name?

Posted: Tue Nov 02, 2021 6:02 pm
by Kitsune64
Hi,thanks

yes I think the table pictures must be global but filenames, settings can be local. I will test it.

-----------------------------------

That's work with a global table for pictures.

Thanks.