How to work with multiple pictures and variables name?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Kitsune64
Prole
Posts: 9
Joined: Mon Jul 12, 2021 3:33 pm

How to work with multiple pictures and variables name?

Post 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
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Kitsune64
Prole
Posts: 9
Joined: Mon Jul 12, 2021 3:33 pm

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

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

Who is online

Users browsing this forum: No registered users and 58 guests