Jump to content

Only load file if it isn't already in cache


HiThere
 Share

Recommended Posts

Hi there

My problem is: when i come back to a state, which I already visited/started, Phaser is loading everything in the geame cache again.

Btw. I'm doing this with an extra state called load, which loads all the necessary stuff for the called level. So I'm not using the preload function in the level directly but this shouldn't be a problem i guess.

How i'm loading:

game.load.audio('music', 'resources/sounds/background-music_3.mp3');

Possible solution:

if(!game.cache.checkSoundKey('music')) game.load.audio('music', 'resources/sounds/background-music_3.mp3'); 

So i could check each time if it already exists, but this seems quite inconvenient to me.

 

The easiest way would be to preload all assets before the game starts, but this would take a while depending on how many assets i'm loading

 

So i was going to write an own load function which automaticly makes these checks for me, but before that i wanted to ask if there exist a Phaser only soulution :D

Thank you very much for the great open source framework and for you answers!

 

Edit: Or should i always clear the cache on a state change and load all assets from scratch?

Link to comment
Share on other sites

This sounds like a code structure issue to me, more than anything else. i can't believe you need to reload all of your assets each time, that just sounds crazy. There must be a large chunk of common assets you could preload properly up front, with a proper loading bar sequence. And then pull in the extras you need before the next level starts (i.e. a second loader)

Client games we create that are > 10MB are always multi-part loaded. We'll do a minimal (~2MB) load first, that perhaps covers an intro, core UI, and the Main Menu. And when the game starts we launch a second load sequence which usually pulls down everything else. Or in a large game, or a multi-mini game set-up, it pulls down assets required for that specific level / minigame only, but once down they're in the cache and not purged again.

When the game ends, and restarts, we skip the mid-preloader states entirely and jump right into the game. Only in very memory constrained situations do we ever purge the cache.

Link to comment
Share on other sites

Thank you for you answer.

Just to clear up a possible missunderstanding, I also don't want to load my assets again. I would like to skip the once i've already loaded, like you would do by completely skiping the mid-preloader.

I completely understand your idea, but i'm not really happy with it. The thing is that every level may use or not use some assets from another level and they can be started in any order. So i don't know whats already loaded. Ofcourse i could check it somehow or preload all the stuff the levels may use once (like you said). If i do so, the user/player has to wait untill all level asstes are loaded even if he only wants to play 1 or 2 levels. Thats why i would like to only load the missing stuff on each level start.

Link to comment
Share on other sites

Then it's up to you to maintain a list of which assets have been loaded, and which are still required.

It still sounds dangerously like over-egging the pudding to me though. Unless we're talking huge quantities (or file sizes) of assets here, then there's no harm in just having them duplicated between the levels.

Easy option first, then if it actually becomes a problem, address it.

Link to comment
Share on other sites

Ok i guess you are right. I only would like to know from you what you treat as a huge kind of data.

From your post above I assume you would not preload ~10 audio files/songs (each 2mb) and additonaly images? I mainly worry about the memory when i'm preloading all the audio files.

Is it also advisable to remove huge audio files like songs from the cache and load them again wehen they are needed?

Thereby i also noticed a "bug", when i'm preloading the sound from my code example above and play it afterwards, it won't start immediatly, but if i start the sound a second time (without preloading again) it starts immediatly. So it seems like the sound preloading isn't working perfectly.

Edit: When i would keep them duplicated between the levels, wouldn't this mess up the cache after a while if i dont delete them from the cache?

Link to comment
Share on other sites

On 5/8/2016 at 7:33 PM, HiThere said:

when i'm preloading the sound from my code example above and play it afterwards, it won't start immediatly

I'm fairly new to Phaser, but I think this is because the audio has to be both loaded and decoded.  Decoding can take a while depending on the device, file size, and encoding format.  There are some properties and methods attached to both Phaser.Sound and Phaser.SoundManager to help manage this (e.g. sound.isDecoded, sound.isDecoding). Some related info here http://www.html5gamedevs.com/topic/13307-audio-starts-up-to-1-minute-late-after-game-is-loaded-and-menu-already-visible/

Link to comment
Share on other sites

There is no definition of 'huge' when it comes to data, because it depends entirely on what you're building, and for which devices and platforms.

Personally I'd never have 20MB of audio in a web based game, especially not one that should run on mobile too. But even so, once you're over the decoding pain of 20MB of data, I'd keep it in memory for as long as I could, to avoid the pain of decoding it yet again. Same with images. I'd always try to keep everything in the cache, and never clear it, ever. As it penalises the end-user and causes duplicate network activity, for no real reason. If you're absolutely running out of memory, and the browser keeps crashing as a result, then you need to do something. Until that point, just keep testing, and testing.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...