Page 1 of 2

removing images

Posted: Wed Feb 26, 2014 5:05 am
by Findo777
On the wiki, it said this; Shape:destroy()

but when I make a image, I do this:

image = love.graphics.newImage("block")
love.graphics.draw(block,400,300)


Now how would I remove it?


Thanks for your help in my first language guys, it is appreciated

Re: removing images

Posted: Wed Feb 26, 2014 5:13 am
by HugoBDesigner
Findo777 wrote:On the wiki, it said this; Shape:destroy()

but when I make a image, I do this:

image = love.graphics.newImage("block")
love.graphics.draw(block,400,300)


Now how would I remove it?


Thanks for your help in my first language guys, it is appreciated
You'll have to use an external variable. You can't "unload" images while using love.graphics, but you can do this:

Code: Select all

[on love_load]
image = love.graphics.newImage("block")

[wherever you'll remove the image]
image = false

[on love_draw]
if image then
    love.graphics.draw(image,400,300)
end
or, if you don't want to loose your image - perhaps you want to use it later:

Code: Select all

[on love_load]
image = love.graphics.newImage("block")
drawimage = true

[wherever you'll remove the image]
drawimage = false

[wherever you'll re-add the image]
drawimage = true

[on love_draw]
if drawimage then
    love.graphics.draw(image,400,300)
end

Re: removing images

Posted: Thu Feb 27, 2014 1:20 am
by Findo777
Sorry... this is not working for me....

Re: removing images

Posted: Thu Feb 27, 2014 1:33 am
by veethree
For a small game there's not really any need to 'remove' images. Just don't draw them.

But if you really need to, I think something like this would completely remove an image from the memory

Code: Select all

image = love.graphics.newImage("whatever.jpg")
image = nil
collectgarbage()

Re: removing images

Posted: Tue Apr 22, 2014 3:11 pm
by kingomar
What if the images are stored in a table, how can i clear the table and free the memory?

Re: removing images

Posted: Tue Apr 22, 2014 4:20 pm
by HugoBDesigner
kingomar wrote:What if the images are stored in a table, how can i clear the table and free the memory?
If the table consists ONLY of images you wanna remove, I think you can pretty much do this:

Code: Select all

imageTable = {}
If only some part of the table is an image...

Code: Select all

imageTable = {x = 10, y = 20, image = image, rotation = 0} --just an example. The image is "imageTable.image"
imageTable.image = nil --or imageTable["image"]
You can use collectgarbage() after it, but I'm not sure if it is REALLY necessary. It takes a bit of time to run, but in the end it helps...

Re: removing images

Posted: Tue Apr 22, 2014 5:17 pm
by Jeeper
I think that you are just misunderstanding things. You do not need to remove images, memory is being handled by the garbage collection and you need A LOT of images before this even makes a small difference.
Findo777 wrote:On the wiki, it said this; Shape:destroy()
That has to do with the box2d module (Physics) and has nothing to do with images.
Findo777 wrote: image = love.graphics.newImage("block")
love.graphics.draw(block,400,300)

Now how would I remove it?
An image is drawn every frame, if you stop calling "love.graphics.draw(block,400,300)" the image will no longer be visible.

An example:

Code: Select all

If player.alive == true then
    love.graphics.draw(player.img,player.x,player.y)
end
This will make it so that the player is visible when he is alive, and the second he is no longer alive, he is not visible.


And when it comes to images the vast majority will be reused later on in a game, so in MOST cases it is actually a very bad idea to try to "unload" an image.

Re: removing images

Posted: Thu Oct 21, 2021 8:26 pm
by pixelprato
Thx guys !!!
settings up my unused images to nil and collectgarbage() works perfectly
:awesome: :) :D ^^

Re: removing images

Posted: Fri Oct 22, 2021 10:09 am
by pgimeno
This is an old thread (2014). Things have changed since; now there is image:release() that you can use to free an image.

Re: removing images

Posted: Mon Oct 25, 2021 5:11 pm
by milon
Note that after calling any :release() function, it's also best to set that variable to nil.