Search code examples
haxeflashdevelop

How to load txt file in haxe in js project?


I've started a haxe js project in FlashDevelop, I need to load a local file, is this possible? how to to so?


Solution

  • The simple answer is use "resources". You add a path and an identifier to your hxml:

    -resource hello_message.txt@welcome
    

    And you use it in your code like this:

    var welcome = haxe.Resource.getString("welcome");
    

    Note that the operation is performed at compile time so there is no runtime overhead. It is essentially equivalent to embed the file content in a quoted string.

    The complex answer is to use a macro. With them you can load, parse, process and do all the manipulation you might need. Pretty commonly, you can see macros to load a config file (say JSON or YAML) and use it as part of your application (again at compile time and not at runtime).