Importing lots of assets

Community Forums/Monkey2 Talk/Importing lots of assets

Leo Santos(Posted 2016) [#1]
Hi,

Working with assets in M2 seems to be more or less like this:

#Import "images/classic_small_bold.ttf"
'...
font = Font.Load( "asset::classic_small_bold.ttf", 12 )


Which is fine, until you have a lot of assets and you're constantly switching them while designing stuff. Since you have to change the name in two places for each asset, you end up doing a bunch of Find&Replaces every time you want to switch multiple assets.

Maybe there's a better way? Let me know! If there isn't here's a suggestion:

How about an #Asset or #ImportAsset keyword? It could work more or less like this:
#Asset variable = "path"


So that you assign the asset to a variable, and later just use that variable. The advantage is that when you point it to a different asset, you only change the path once, and all the code using the variable gets the change, like this:
#Asset classicFont = "images/classic_small_bold.ttf"
'...
font = Font.Load( classicFont, 12 )


Internally, classicFont means "asset::classic_small_bold.ttf", so nothing changes on that end.

Anyway, the basic idea is just to assign an asset to a variable, I'm sure it would need to be modified to fit M2 standards.

P.S. An alternate usage that probably fits better would be :
font = Font.Load( "asset::classicFont", 12 )


dawlane(Posted 2016) [#2]
I haven't played are with the importing of assets. Has Mark implement the use of wild cards within the import directive so #Import "*.ttf" would import all ttf files?
I myself would have imported the files and then parsed the asset directory for those file names into a stack container.


Leo Santos(Posted 2016) [#3]
I tried:
#Import "images/*.ttf"

And got this: Build error: Unrecognized import file filter '*.ttf'
I don't think it would be ideal anyway, since it would potentially add a lot of unused files to the build.


marksibly(Posted 2016) [#4]
The plan is indeed to add a file filter, but I haven't got around to it yet.

> I don't think it would be ideal anyway, since it would potentially add a lot of unused files to the build.

Not if images/ only contains files you wanted in the build!

The basic idea is to be able to emulate mx1 behaviour with something like: #Import "data/*"


dawlane(Posted 2016) [#5]
>Not if images/ only contains files you wanted in the build!
Mark said it before I did.
With the right kind of file naming and use of wild cards e.g my*asset_level*.png. You can do a bit of filtering out.


Leo Santos(Posted 2016) [#6]
Not if images/ only contains files you wanted in the build!

That's a good thought, but in reality this is very prone to error in complex projects (i.e hundreds or thousands of assets). Lots of AAA games ship with unused assets due to their complexity and the difficulties managing that sort of thing.

In an ideal world, checking if an asset is actually used would be wonderful! I think Unity3D does something like that. Maybe some sort of post-build stripping? If you had that, you could effectively use the asset folders as design folders, which is great as long as you can keep your folders named and organized. But hey, I'm speaking from the perspective of an artist working in a huge art pipeline here. :-)

Of course that would be a long term goal. In the meantime, improving the existing import system would be very welcome!


marksibly(Posted 2016) [#7]
> In an ideal world, checking if an asset is actually used would be wonderful!

This is kind of impossible to do in general as file names can be dynamically generated, eg:

LoadImage( "levels/"+currentLevel+"/bossMonster.png" )


Leo Santos(Posted 2016) [#8]
True, but isn't that only if you're loading the images directly?
If you're "preloading" them as assets before using them shouln't that be possible?

Anyway, that wasn't what I was asking originally! :-)
The import wildcards would be welcome, but I just want to avoid typing the asset name more than once by assigning it to a variable when loading a specific asset. Thanks!


marksibly(Posted 2016) [#9]
> If you're "preloading" them as assets before using them shouln't that be possible?

There isn't any 'preloading' in mx2 - you need to LoadImage() etc what you want to use.


Leo Santos(Posted 2016) [#10]
Oh, that's interesting to know. Can I get away without the #Import "file.png" and load it directly, then? Or is importing assets necessary to copy all assets to the build folder?
Thanks!


dawlane(Posted 2016) [#11]
> Oh, that's interesting to know. Can I get away without the #Import "file.png" and load it directly, then?
Assuming that you have an asset directory already created where the executable will be built. Or explicit paths when Loading.

> In an ideal world, checking if an asset is actually used would be wonderful! I think Unity3D does something like that. Maybe some sort of post-build stripping?
Unity is basically an all in one editor and would have a built-in asset manager.
In theory an IDE written for mx2 could have a built in asset manger that generates it's own file of #Import of assets and main code files.

Edit:
You would of course have to write your own internal asset manager to handle the Loading etc of the assets for use.


marksibly(Posted 2016) [#12]
> Can I get away without the #Import "file.png" and load it directly, then?

Without the #import, assets wont be copied to the output 'assets' dir, but in general, yes, you can load from the filesystem as long as you have paths, current dir etc right.


dawlane(Posted 2016) [#13]
As a side note.
In MX1 you could store all your assets in the myapp.data directory. You could use the compiler directives
#TEXT_FILES="*.txt|*.xml|*.json"
#IMAGE_FILES="*.png|*.jpg"
#SOUND_FILES="*.wav|*.ogg"
#MUSIC_FILES="*.wav|*.ogg"
#BINARY_FILES="*.bin|*.dat"
along with #If TARGET #Else #EndIf to do a bit of filtering of those assets.


therevills(Posted 2016) [#14]
In my last MX1 project, Chef Solitaire: USA Tri Peaks (link), had 132 files in the data folder... what is the current (best?) way to access that amount of files (images, sounds, json etc) in MX2?


marksibly(Posted 2016) [#15]
The best way currently is probably to skip #Import and use the filesystem.

Adding a simple #Import "blah/*.*" etc shouldn't be hard though and I plan to get it in before V1.0 as I imagine it'll start sucking having to #import every individual file real fast.


therevills(Posted 2016) [#16]
In my recent games I've been using an external file such as xml or json which I updated and get BMX/MX1 to to read and load in the resources... just checking, but will this still be possible?