Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by dungeonmaster

I have 1000 different images for entities in Level1. Then I move to Level2 and some of these images are now useless, but I don't keep a track of which are those.

How can I completely clear Image cache?

Is there any way to clear images from cache if it's not being used by any entity?

And if there is a way should I expect an increase in performace?

1 decade ago by Graphikos

There is nothing specific to Impact to handle cache. This is all up to the browser. So clearing the cache would be simply clearing the browser's cache.

Maybe you are talking about memory usage? Even a lot of that (garbage collection) depends on the browser.

1 decade ago by dungeonmaster

I'm referring to the resource caching in impact.

When you want to load a png with the same name second time, impact actually uses the first cached image and doesn't load from file again.

Is there any way to delete that cached image automatically if it isn't being used in any entity?

1 decade ago by molocLab

Is there any way to delete that cached image automatically if it isn't being used in any entity?


You can try to add a random parameter to the image path, something like:
image.png?Math.random()*9999

1 decade ago by Graphikos

Quote from dungeonmaster
I'm referring to the resource caching in impact.

When you want to load a png with the same name second time, impact actually uses the first cached image and doesn't load from file again.

Is there any way to delete that cached image automatically if it isn't being used in any entity?


Still... it's the browser that is caching it... not impact. Doing what molocLab suggested would force the browser to load it again. Essentially giving it a different name forcing the browser to not assume it's the same resource.

There is no "automatic way to delete unused resources" and I'm not really sure why you need to concern yourself with it.

1 decade ago by dominic

I guess dungeonmaster is refering to Impact's internal cache of ig.Image instances. This cache is filled when an image is loaded, so that the ig.Image instance is ready when it's requested a second time in the same game. This is independent of the Browser's cache. It will start completely empty each time the site is loaded.

You can clear the cache by assigning an empty object:
ig.Image.cache = {};

If there are no references to the same ig.Image anywhere in your code, the browser should garbage collect the images.

However, typically all ig.Image instance are loaded once with the pre-loader before the game starts. So all images your game needs are already loaded in level 1. Also, there likely will be no performance increase at all when clearing the cache.

1 decade ago by dungeonmaster

Thanks alot for the answers. Why I'm asking the question is that I injected this code:

	ig.Image.inject({
		init: function( path ) {
			this.path = path;
			//If image is dynamically created, there is "_" before filename.
			if (path.charAt(0)=== '_') {
				this.loaded = true;
				this.failed = false;
				ig.Image.cache[this.path] = this;
			} else {
				this.load();
			}
		},
	});

So in my game I'm actually creating the graphics dynamically (without .png) during the level and caching them using ig.Image.cache.

This speeds up the draws when compared to direct drawing on canvas but I'm concerned with the ever-increasing cache size.

But anyway, if the image cache is specific to a game, then it's OK because I'm creating a new game for each level anyway.

Dominic; the browser doesn't seem to garbage-collect unsued caches. I think even no entity has that image, the reference held by ig.Image.cache is enough to keep garbage collectors away...I guess
Page 1 of 1
« first « previous next › last »