About Monkey 2 › Forums › Monkey 2 Programming Help › how to check if an asset exists
This topic contains 9 replies, has 6 voices, and was last updated by
degac 1 year, 2 months ago.
-
AuthorPosts
-
February 2, 2018 at 9:28 pm #13426
I’d like to detect if an asset if present in order to send an error message. But I suppose filesystem functions are not valid for that.
Is there another way than checking if the Loadxxx() function returns null?February 3, 2018 at 1:27 am #13432I do the following in Diddy2:
Monkey1234567Local image:Image = Image.Load(path)If Not imagePrint("Error: Can not load image: " + path)App.Terminate()EndFebruary 3, 2018 at 7:06 am #13434Quick question, is it possibile to use GetFileType(path) to known if that file exists or not? It should be faster
Not tested, just thinking…edit – tested using RenderToImage example
Monkey1234567Local ft:=GetFileType(AppDir()+"/assets/spaceship.png")Select ftCase FileType.File Print "File"Case FileType.Directory Print "Folder"End SelectOf course it would be better creating a ‘CheckForFile’ function
February 3, 2018 at 7:31 am #13435AppDir() is not cross platform and will not always return the folder you think it will.
Windows and mac’s have different app structureFebruary 3, 2018 at 7:57 am #13437Ahhh…. Good to know.
But at this point what’s the sense of having a command like AppDir (and I suppose AppPath) that returns ‘not sure’ results?will not always return the folder you think it will
What does it means?
That on my computer and your computer (supposing both running Windows10) there are different returned path?I suppose internally Image.Load() will ‘found’ the correct path in some way (and surely is in the /assets folder of the final application)… so replicating this should be the correct solution I think.
Question: when MX2 compiles/build everything it does *just* copy any file/folder defined with #Import and (looking at the source code) it seems to ‘add’ to product.ASSET_FILES any single ‘resources’ (=Path/file).
I don’t know if that ‘collection’ is ‘public’ to the application (so it would be easy to check if something is missing or not).February 3, 2018 at 10:56 pm #13452What about trying AssetDir?
http://turdus.be/monkey2docs/docs/modules/std/module/std-filesystem-AssetsDir.html
Monkey12345std:std.filesystem.AssetsDirFunction AssetsDir:String( )Gets the filesystem directory of the app's assets directory.Note that only the desktop and web targets have an assets directory. Other targets will return an empty string.February 4, 2018 at 12:08 am #13457I looks like I’ll have to check for Null result.
Thx
February 4, 2018 at 12:33 am #13458Checking for null kinda works, but is not ideal.
Sometimes you get null when there’s a file format problem, which is common when dealing with various 3d formats that are not entirely standard, like FBX and its multiple iterations over the years. In cases like that, it can take longer than necessary to figure out where the problem is (file name wrong? format error? typo? did I forget to put it in the right folder? etc.).
Ideally, attempting to import a non-existent asset should cause a specific errors, at least on Debug mode.
Cheers.February 4, 2018 at 1:28 am #13459AssetsDir()+path etc will work for most targets, it’s only android that causes problems as assets are stored in a zip file on android. On all other targets, assets are just files in the filesystem, so GetFileType etc will work.
What will work safely is opening the asset as a stream, eg:
Monkey12345678910111213Function AssetExists:Bool( path:String )If Not path.StartsWith( "asset::" ) path="asset::"+pathLocal stream:=Stream.Open( path )If Not stream Return Falsestream.Close()Return TrueEnd(untested).
February 4, 2018 at 8:38 am #13461On all other targets, assets are just files in the filesystem, so GetFileType etc will work.
Looking at the source code in AssetsDir() seems that
1. documentation should be updated as target IOS seems to ‘return’ the correct path. So the problem is only for ANDROID
2. on ANDROID target it would be nice (as the function returns a string) to just put in the debug a ‘warning’ about the use of that function.in any case I think that even ANDROID_TARGET should return a string about the internal ‘assets’ folder. Then is up to the developer – if wanted – use that path to access/open the .zip structure to read/check the asset.
-
AuthorPosts
You must be logged in to reply to this topic.