Compiler flag called "tools" and adding assets to Assets.cache

I have some images that are being loaded at runtime and store them as BitmapData instances. Since most of my old libraries make use of openfl.Assets.getBitmapData() to get images, I thought it would be neat to just add the BitmapData objects to the Assets class’ cache and make them accessible that way. So I’m using Assets.cache.setBitmapData() to put images into the cache. The problem is that retreiving the images only works directly from the cache:

Assets.getBitmapData(id)          // returns null
Assets.cache.getBitmapData(id)    // returns the BitmapData I stored earlier

Then I looked at the source of Assets.getBitmapData and saw it checks 2 compiler flags like this:

#if (tools && !display)
// Imagine the actual code that would return the BitmapData from the cache here.
return null;

To test it I put some traces in there and it turns out the inside of that conditional compilation if statement thingy never gets executed. That’s why it returns null. What are those “tools” and “display” compiler flags? I tried googling them but they’re such generic words it’s been hard to come up with any useful results. “display” is listed in the haxe documentation but all it says is “Activated during completion”. Not sure what that means. “tools” isn’t listed there so I assume it’s defined by openfl or lime?

“Activated during completion” means “activated during code completion.” The main use of that flag is to cut out code that isn’t required for code completion.

The “display” flag is false when compiling for real, meaning “tools” has to be the culprit. Looks like the entire lime.Assets class depends on that flag, and openfl.Assets depends on lime.Assets.

I can’t find where the tools flag is set, so I don’t know how you’re supposed to enable it. I guess you could try setting it yourself (<haxedef name="tools" />), but who knows what side effects that would cause?

Another option is to enable legacy mode (<set name="openfl-legacy" />), but that seems like overkill when the real solution is probably very simple.

“display” is enabled when the Haxe compiler is doing code completion

“tools” is enabled when building a project using the Lime/OpenFL command-line tools

Perhaps you should look at using a custom asset library? This would cleanly add additional files to the asset system at runtime.

Call “registerLibrary” with your custom instance:

Assets.registerLibrary ("my-stuff", new MyCustomAssetLibrary ());

Then you can call things like:

var bitmapData = Assets.getBitmapData ("my-stuff:MyCustomImage.png");