Loading new assets after compiling

  • Hello !

    I am currently making a little game based on very simple mods.
    Basically, users can put assets (sprites, sounds and scripts) in a folder and mod the game using JSON files.


    • main.json (modding):
    "sprite": "tsubakura.png",
    • Setting the sprite (source code):
    loadGraphic(mod.Path + "img/" + JSONdata.sprite ...
    > 'mods/test2/img/tsubakura.png'

    I created a "mods" folder next to the assets, and I added the path in the XML file:
    <assets path="mods" />

    I did some tests, and apparently, by default, the AssetPath class is loading the assets... But only those referenced during compilation.
    If I change the name or add a new one (after compilation), the console says that it doesn't find the asset.
    For example: [ERROR] Could not find a BitmapData asset with ID 'mods/test2/img/tsu.png'
    (I changed the name of the file, I updated the JSON file. The file is here but not recognized)
    But when I change a file while keeping the same name, it's loading as expected.

    So my question is: Is there a way for the AssetPath class to load all of the assets in the "Mods" folder when we launch the game (in native platforms) ?

    I think that this post can be confusing (I'm still a beginner in Haxe and English is not my first language), so don't hesitate to ask me for details.

    Thank you!

  • administrators

    The OpenFL asset system / openfl.Assets indeed only works for assets included at compile time. This is to allow for consistent code across targets that do have file system access and those that don't. Also, there's an option to embed assets.

    What you're looking for are the following methods:

  • Thank you for your reply! Scripts and sprites are working, but now I don't really know how to load music and sounds...
    I know that openfl.media.Sound.fromFile() can be used to create a FlxSoundAsset, but it appears to only work in the Flash target...

    Can I create an FlxSoundAsset from a "non-AssetPath" Ogg file (in native platforms) ?

  • administrators

    @mobandon I would expect Sound.fromFile() to work, what issues are you having exactly, and what OpenFL version are you using?

  • @gama11 It's working alright with Sound. Maybe I will try to improve all of this later, but it's good enough for the moment. Thank you for your help!

  • administrators

    @mobandon I'm confused, didn't you say it wasn't working for ogg files / native? Or did you manage to solve that?

  • @gama11 I didn't test Sound.fromFile() before, I was just a little confused because the OpenFL docs say that The Sound class lets you create a Sound object, load and play an external MP3 file.

  • administrators

    @mobandon Ah, I see. That's probably because OpenFL's API docs are based on the ActionScript 3 / Flash API, where MP3 was the only supported audio format I think.

Log in to reply

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.