Receive project path in macOS from Lua (SOLVED)

Hi all.

In my project I recieve some data from external files. In Windows project read files from application dir by default, but in macOS no and required absolute path. My question - how recieve current dir in macOS. I know about os.getenv(“HOME”) , but it return user home, but i need app dir. Thank you

What do you need app dir for? You shouldn’t write to the application directory but to user data folders.

I need read some PNG files (long animation sequences). I dont need write for current moment :slight_smile:

There are a couple of ways to do it and I’m not sure the best way… you can make a native extension to use built in osx functions to get it

std::string([NSBundle mainBundle].executablePath.UTF8String)

https://developer.apple.com/documentation/foundation/nsbundle/1409078-executablepath
https://developer.apple.com/documentation/foundation/bundle/1409078-executablepath

Would return full path to the engine exectuable I think.

You can also do it based on PID.

I don’t have a Mac handy at the moment to test.

Keep in mind it’s non-trivial to get access to the files Defold packs into the game’s resource files. If you add the files to the .app file it’s no problem.

It would probably be useful if someone made a “where am I” native extension for the various platforms.

1 Like

sys.save() and sys.load() works on the application root on OSX. So does io.open().

You mean like lfs.currentdir()? :wink: (https://www.defold.com/community/projects/76577/ and https://keplerproject.github.io/luafilesystem/manual.html#reference)

2 Likes

Thank you guys. I think LFS resolve my problem.

defos.get_bundle_root() also does this now (thanks to @AGulev).

3 Likes