removing images

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

removing images

Post 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
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: removing images

Post 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
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

Re: removing images

Post by Findo777 »

Sorry... this is not working for me....
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: removing images

Post 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()
User avatar
kingomar
Prole
Posts: 37
Joined: Mon Mar 17, 2014 8:01 pm

Re: removing images

Post by kingomar »

What if the images are stored in a table, how can i clear the table and free the memory?
Check out my game:
- Youtube Video : http://goo.gl/91x6dT
- Download Link : http://goo.gl/6llhmp
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: removing images

Post 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...
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: removing images

Post 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.
User avatar
pixelprato
Prole
Posts: 2
Joined: Wed Sep 15, 2021 10:18 am
Contact:

Re: removing images

Post by pixelprato »

Thx guys !!!
settings up my unused images to nil and collectgarbage() works perfectly
:awesome: :) :D ^^
User avatar
pgimeno
Party member
Posts: 3657
Joined: Sun Oct 18, 2015 2:58 pm

Re: removing images

Post 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.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: removing images

Post by milon »

Note that after calling any :release() function, it's also best to set that variable to nil.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests