threedollarbill Posted July 18, 2016 Share Posted July 18, 2016 How come I cannot access a binary file from the cache? The documentation says that it gets added to the cache after being loaded. I am able to access the data and do whatever I want with it, there's no problem with that. But I just can't access it from the cache, and I'm wondering why. My code goes something like this.. // create a new loader and begin loading... let myLoader:Phaser.Loader = new Phaser.Loader(this.game); myLoader.binary("myBinaryKey", "url/to/my/binary/file", this.onBinaryLoaded, this); // this is the function that is run when the binary file is loaded private onBinaryLoaded(key:string, data:any):void { // attempt to get binary file via cache key let myBinaryFile:any = this.game.cache.getBinary(key); // <-- this outputs a "key not found in cache" warning // output data to the console console.log(data); // <-- this successfully outputs the data to the console.. } Link to comment Share on other sites More sharing options...
drhayes Posted July 19, 2016 Share Posted July 19, 2016 I'm not clear what you're doing. There's a chance that your callback is getting called before the data is in the cache... but that's okay, because the second arg to your callback is the data you're trying to get from the cache. Is the binary not appearing in the cache once preloading is complete? Link to comment Share on other sites More sharing options...
Tom Atom Posted July 19, 2016 Share Posted July 19, 2016 Hi, I think, you have to return something from callback. I am using this: // level data this.load.binary("LevelData", path + "levels.bin", this.onBinaryLoaded, this); and this: // ------------------------------------------------------------------------- public onBinaryLoaded(key: string, data: ArrayBuffer) { return data; } and I can use it in code like this: var levelData = <ArrayBuffer>this.cache.getBinary("LevelData"); Link to comment Share on other sites More sharing options...
threedollarbill Posted July 20, 2016 Author Share Posted July 20, 2016 On 7/19/2016 at 10:36 PM, drhayes said: I'm not clear what you're doing. There's a chance that your callback is getting called before the data is in the cache... but that's okay, because the second arg to your callback is the data you're trying to get from the cache. Is the binary not appearing in the cache once preloading is complete? Yes, even after loading has completed, the binary file cannot be found in the cache when trying to access it via its key. The reason why I am trying to do this is because I would like to prevent memory leaks by removing binary files that are no longer being used by my application. The code I originally posted is a simplified version of what I am trying to do. This is what my onLoadBinary function actually looks like. I am basically loading WebP images via the binary loader, decoding them, and creating sprites out of them. private onWebPBinaryFileLoaded(key:string, data:any):void { // convert binary data to array let dataArray:Uint8Array = new Uint8Array(data); // decode image data and create bitmapdata from it let imageData = window.DWP(dataArray); let bmd:Phaser.BitmapData = this.game.add.bitmapData(imageData.width, imageData.height, key, true); bmd.ctx.putImageData(imageData, 0, 0); // create sprite and add it to the display let imageSprite:Phaser.Sprite = this.game.make.sprite(this.currentImageXPos, 0, bmd.texture); this.imagesContainer.addChild(imageSprite); // store record of key this.currentKeysArray.push(key); // <-- I AM STORING THE KEY HERE FOR USE LATER } Later on in my application, once I no longer need the binary file / data, I want to make sure that it is no longer in memory by attempting to look for it in the cache and if found, remove it. I do something like this. private clearPreviousBinariesFromCache():void { // remove binary from cache if found for(let theKey of this.currentKeysArray) { if(this.game.cache.checkBinaryKey(theKey)) { this.game.cache.removeBinary(theKey); console.log("found and removed binary!"); // <-- THIS NEVER FIRES AND NO BINARY FILES EVER SEEM TO BE FOUND.. } } // destroy and remove bitmapdata if found let targetBMD:Phaser.BitmapData; for(let theKey of this.currentKeysArray) { if(this.game.cache.checkBitmapDataKey(theKey)) { // destroy bitmapdata targetBMD = this.game.cache.getBitmapData(theKey); targetBMD.destroy(); targetBMD = null; // remove bitmapdata from cache this.game.cache.removeBitmapData(theKey); console.log("found and destoyed bmd!"); // <--- THIS FIRES, AND I AM ABLE TO DELETE BMDs } } // reset key array this.currentKeysArray = []; } Link to comment Share on other sites More sharing options...
drhayes Posted July 25, 2016 Share Posted July 25, 2016 I'm sorry, I wish I had a better idea of what is going on, but I don't. Link to comment Share on other sites More sharing options...
Recommended Posts