Working directory of commands

I have noticed that the working directory may not be set correctly when using Commands.

I added a command to start my game. The Command consists of the full path to my game executable.
The tilemap I am currently working on resides in a subfolder “assets/tilemaps”. My textures are in a subfolder “assets/textures”.

When I press F5 to start the command, my game executable immediately crashes, because it is trying to find the textures in “assets/tilemaps/assets/textures”, which of course doesn’t exist.

I would assume that Tiled does not set the working directory to the executable’s directory, but rather just starts it. As a result the working directory for the executable remains “assets/tilemaps” = the folder the current map is residing in.

Could this be changed?

It seems logical to have the working directory be the same directory as the TMX map the command should work with.

In case you don’t know, you can actually backtrack from this folder if you have to. So instead of “assets/textures” in your tool you could specify “…/textures” to backtrack into the assets folder and then into the textures folder. This should work on all operating systems and APIs though the approach may differ. For instance it may be necessary to first resolve the relative path to an absolute path using an OS routine. On OS X paths like “…/” are transparently expanded, and you can even do things like “some/path/…/…/other/path”. But if memory serves me well this used to be a problem or simply non-intuitive on Windows.

Actually at the moment no explicit working directory is set on the commands, which means they run in the same working directory used when executing Tiled. You can work around this by creating a shell script (or .bat file on Windows) that changes the working directory before continuing to run your game.

I’m not convinced that this would generally be helpful, but it could be made an option.

There’s an open task to allow setting the working directory. I would introduce some magic variables there, like %executablePath and %mapPath, for getting the behaviors you guys are suggesting. The task is: