Accessing Embedded files

I’m messing around with embedding files and I am curious how do I get the assets that are embedded? Like, say I wanted to get an asset ./assets/background.png and the ./assets/ folder is embedded?

If I’m completely misinterpreting what embedding a file actually does, I’m sorry

in project.xml you add this to embed your images that are in assets/images folder
<assets path="assets/images" include="*.png" embed="true" />
and in code it would be "assets/images/background.png"

also you can rename the path (an alias) you would use in code like this
<assets path="assets/images" rename="img" include="*.png" embed="true" />
and in code it would be "img/background.png"

In the case of a windows build, when you embed the images, or other assets, they are embedded in the exe itself and no folders are in the build. For HTML5 the folders and files are included in the build, but you access them the same way.

Hope it helps.

I was more of meaning how do I actually access them because the file paths for the files aren’t actually there anymore. Using your example /img/background.png/ doesn’t work because that isn’t a valid path?

You need to use the Assets class to access the assets that you embedded.

Something like this, I believe: Assets.getBitmapData("assets/background.png")

If using that path as the asset id isn’t exactly correct, you can check the result of Assets.list() to get the proper id.

2 Likes

Using paths over id can be desirable when you have got tons of images and you don’t want to assign each one of them with an ID and also making the project.xml bulky and overwhelming.

The restricted syntax required to use the path (instead of id ) is what make it confusing most of the times. Often the confusion is about using “.” , “/” or both etc.

This won’t work even if the build has got the assets folder along with required images

<assets path="./assets"  />

For example using

Assets.getBitmapData('assets/pic.png')));

will generate error

You must use rename attribute too.

<assets path="./assets"   rename="assets" />
Assets.getBitmapData('assets/pic.png'))); //will work now

Also these too won’t work because of leading “.” and “/” or both

<assets path="./assets"   rename="./assets" />  <!-- error -->
<assets path="./assets"   rename="/assets" /> <!-- error -->

Inside the haxe code

    Assets.getBitmapData('assets/pic.png') // works

    Assets.getBitmapData('./assets/pic.png') //ID not found error 

    Assets.getBitmapData('/assets/pic.png') //ID not found error