Asynchronous to Synchronous resources

Hi,
I’d like to know if I can preload remote resources with the asynchronous load### functions and use them later synchronously by getting them with get### functions, without saving the contents I receive when loading is complete.

I other words, is it possible to “convert” at runtime non-embedded resources to embedded-resources?

I tried (i.e. Assets.loadSound(“bang.mp3”, true), loading completes and later I use getSound(“bang.mp3”)) but it tells me the resource must be loaded asynchronously, as if it hasn’t been downloaded yet, even if the “cache” parameter of the load function is set to true. Am I using the wrong method to get the downloaded resources? Do I have to save all resources apart during the preload phase? It would be very uncomfortable.

It should work this way, so let’s hunt down cases where this might not be true. This use-case hasn’t been tested much, but I’d like to see it happen.

Perhaps this is an issue between lime.Assets cache and openfl.Assets cache, it is cached in lime.Assets, but not openfl.Assets, so it doesn’t return, so we just need to get that synced

Do you think there could be a different behaviour in HTML5 and Flash targets?

Hmm, I’m not sure. We probably need to check the lime.Assets cache, and if it is cached there, use that as the source for loading. Otherwise, we also need to make sure that clearing the OpenFL cache can clear the Lime cache (I thought it did, but its worth checking again)

So, i.e. I have to use

lime.Assets.loadImage
lime.Assets.getImage

instead of

openfl.Assets.loadBitmapData
openfl.Assets.getBitmapData

to see cache working?

You could try that, but really, I need to take the time to make a test project for this, and work out the little kinks here

I think I have found where the problem is: it looks like I can use Assets.loadBitmapData and get the image data later with getBitmapData as if it was embedded, because cache works correctly for images (well, it is a parameter of the function :smiley: ), but NOT for XML files: loadText has no parameter “useCache”, so I can’t preload it to use later.
I think the fix is pretty easy, but I should have a look at how cache works.

EDIT: Ah! In Assets.hx - AssetCache I see

bitmapData = new Map<String, BitmapData> ();
font = new Map<String, Font> ();
sound = new Map<String, Sound> ();

…this means that XMLs cannot be cached?! Neither lime.Assets caches XMLs!
What about adding

text = new Map<String, String> ();

?

EDIT2: looking at lime.Assets: seems like xml files MUST be cached.

EDIT3: openfl.Assets modified to allow text caching! Now I have a local cache in singleton openfl.Assets.hx for texts too! Ignoring lime.Assets “cache”.

Currently, here’s how it works:

BitmapData, Font and Sound resources are class instances. They can be shared and re-used multiple times. There is a local cache in Assets to allow repeated requests, without creating duplicate instances of the same data.

For ByteArray and String resources, they are more “raw” and often are manipulated after use. In the case of String, a cache would probably make little sense? (these types of files are also less likely to be used repeatedly)

On HTML5, when embedded, these are preloaded, and pulled in internally. I suppose in the loadText or loadBytes sense, it could be nice to have them cached, but can you just flag your XML file as embedded? Is it defined at runtime?

Xmls are set as non-embedded
If I try to load an xml with loadText and then get it with getText it tells me I can only get it asynchronously -> by loadText.
Maybe a bug in comunication between openfl.Assets and lime.Assets: openfl.Assets.getText calls lime.Assets.getText, but lime Assets responds the file is not cached, even if I loaded it.

I don’t need to reuse the text files, I just need to load only files version I need and use them later: I have 2 versions, mobile or desktop.

Hi

Any news about text cache ?

I’m using haxeflixel (so openfl v 3.6.1), targetting flash/desktop and i must load async json files in my project. (can’t embbed it)

I try to load a .json with loadText method :

	private function loadText(file : String)
	{
		var future : Future<String> = Assets.loadText(file);
		future.onComplete(onTextLoaded.bind(_, file));
		future.onProgress(onCurrentlyProgress.bind(_,file));
		future.onError(onError);
	}

	private function onTextLoaded(data : String, file : String)
	{
		trace("Text succesfully loaded:" + file);
		nextLoading();
	}

When i use Assets.getText(), i have the error : “String asset “assets/data/test.json” exists, but only asynchronously”

I test it with loadBitmapData/getBitmapData without issue (all work perfectly)

Any correct solution to fix it without change code into lime/openfl locally ? (like a wrapper or using an assets library/provider)

I found some library but i prefer to manage assets with only 1 system :


Thx for reply :slight_smile: :

I am using subclasses and modified classes while keeping the original ones, and this works as expected. Well I’d like this to be integrated in the official openfl release, but for now it does what I need.