how to check if an asset exists

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 3 months, 2 weeks ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #13426

    abakobo
    Participant

    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?

    #13432

    therevills
    Participant

    I do the following in Diddy2:

    #13434

    degac
    Participant

    Quick 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

    Of course it would be better creating a ‘CheckForFile’ function

    #13435

    AdamStrange
    Participant

    AppDir() is not cross platform and will not always return the folder you think it will.
    Windows and mac’s have different app structure

    #13437

    degac
    Participant

    Ahhh…. 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).

    #13452

    therevills
    Participant

    What about trying AssetDir?

    http://turdus.be/monkey2docs/docs/modules/std/module/std-filesystem-AssetsDir.html

    #13457

    abakobo
    Participant

    I looks like I’ll have to check for Null result.

    Thx

    #13458

    Ethernaut
    Participant

    Checking 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.

    #13459

    Mark Sibly
    Keymaster

    AssetsDir()+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:

    (untested).

    #13461

    degac
    Participant

    On 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.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.