Module assets

Monkey Forums/Monkey Programming/Module assets

Leo Santos(Posted 2015) [#1]
Suppose I have a module mymodule that requires a set of standard assets - let's say, a series of .fnt and .png files with a default font in several different sizes, for example. I can always copy those assets to each individual project that uses the module, but that's very redundant and prone to error, like forgetting to update all the copies every time the originals are updated.

How can I import said assets from a mymodule.data folder inside the module, so I can have them always available to any project that imports mymodule?

I've tried many different ways, but none seems to work. I guess I can use symlinks in OSX and Linux, but I wanted a way that works in Windows as well. Thanks!


ImmutableOctet(SKNG)(Posted 2015) [#2]
Use 'Import' on a supported file (Image, sound, etc) from the module, and it will automatically copy to the main build folder of your project. To see this in action, check out the 'mojo' module's 'data' folder.

Mojo automatically copies its default font; snippet from 'mojo.graphics':


That seems to work as you're describing. Here's the current "mojo" folder on GitHub.


Leo Santos(Posted 2015) [#3]
What if I want to determine what gets imported in the project, not in the module?

Maybe this isn't practical, but what I'm trying to create is a sort of standard Asset Library that can be used in different projects. Each project should be able to determine what they actually need, and ignore the rest. If the module imports everything in the asset library, a lot of unnecessary files would end up in each project.

Thanks!


ImmutableOctet(SKNG)(Posted 2015) [#4]
You can import based on preprocessor variables. This applies to both modules and assets.




Leo Santos(Posted 2015) [#5]
Ah, awesome. Thanks!

[edit]

Oh Wait! This doesn't actually solve the problem! You'd still have to have the import happening in the module itself, no? What I want to do is, from any arbitrary .monkey file in any arbitrary folder, do something like "import mymodule/images/image.png", and it would retrieve the image from mymodule.data, instead of the "current" data folder.

I guess that can't happen because in reality it is simply retrieving the image from the build/data folder, and all files have to be already copied to this folder, regardless of where the images are coming from and regardless of whether they're actually used or not.

Maybe what I really want is a way to determine what files are necessary at compile time, and only copy to the build folder the files that are actually necessary. Monkey 2, maybe? ;-)

Thanks!


itto(Posted 2015) [#6]
Issues like this can easily be solved with a build system. You will have somewhere a config file containing settings for the project and directives for the steps to take in the build sequence. In your case, a list of assets required to be included, or better yet, a list of modules whose required files you want included (specified in a separate list, module per module), and they will be processed as needed (in your case, copied in the data folder, automatically).


bitJericho(Posted 2015) [#7]
Symlinks are available in Windows and I use them quite a bit. For example, before I used jungle IDE, I used symlinks to keep my custom monkey modules in a different location than the normal monkey folder. I use the wonderful Link Shell Extension to use symlinks: http://schinagl.priv.at/nt/hardlinkshellext/linkshellextension.html

Do note that this program also modified windows copy/move behavior with a feature called smart move. I recommend disabling that, to do so, see: http://schinagl.priv.at/nt/hardlinkshellext/hardlinkshellext.html#configuration


Leo Santos(Posted 2015) [#8]
Issues like this can easily be solved with a build system

Sounds great, is that something that can be done currently in Monkey?

I used symlinks to keep my custom monkey modules in a different location than the normal monkey folder.


Yup, that's what I'm doing now. And I'm also using symlinks to point to specific items in an default asset library - instead of copying the assets, I just create symlinks for them. Works pretty well, actually, since Monkey simply copies the original files to the build folder, and I can avoid unnecessary files from the library. But I'd rather it worked automatically, with Monkey identifying which files are being loaded and actually used and only copying those to the build.

In OSX, I use this to create symlinks from the Finder: http://seiryu.home.comcast.net/~seiryu/symboliclinker.html
It's slightly annoying that it appends the name of the file with a "symlink" text.
Thanks!


itto(Posted 2015) [#9]
Sounds great, is that something that can be done currently in Monkey?

Nope, as much as I would like to, Monkey's build system is currently single-pass and there are no hooks or other ways to execute custom commands middle way (It's being discussed as an enhancement for Monkey 2 though).

You should be fine writing a simple shell script which parses a list of strings (module filenames) in a config file, and for each one copies all the assets folder relative to that module folder to the final build folder.