Indicators of being ran in editor vs ran by bundled version

What would be the best way to detect if a build was ran through the editor (project->build) ?

Hmm, good question. Not sure if this can be done. Well, maybe. Perhaps look at the current directory? Or maybe something from os.env()?

Any idea on how to best do this? Now that I think about it again, maybe a way to do it would be to check if a file exists within the project directory which would not be specified to be included in the bundled version. That plus checking if it’s on a desktop OS first maybe so on platforms with an editor version it’s not checked.

Checking the existence of a file or checking the current directory should work reasonably well.

Here’s a simple method that seems to work. The context is needing a path to the bundle resources folder so that only one copy of files are needed in project directory.

local M = {}

--- Check if a file or directory exists in this path
function M.exists(file)
	local ok, err, code = os.rename(file, file)
	if not ok then
		if code == 13 then
			-- Permission denied, but it exists
			return true
		end
	end
	return ok, err
end

--- Check if a directory exists in this path
function M.isdir(path)
	-- "/" works on both Unix and Windows
	return M.exists(path.."/")
end

return M
local files = require("main.files")
local sys_info = sys.get_sys_info()

local function is_editor()
  if not (sys_info.system_name == "Windows" or sys_info.system_name == "Darwin" or sys_info.system_name == "Linux") then
    return false
  end

  if files.isdir("bundle_resources") then
    return true
  else
    return false
  end
end
...

  local extra_path = "."

  if is_editor() then
    extra_path = "./bundle_resources/common"
  end
2 Likes

I’m looking to do this as well. Just checking if you are still using this solution?

What about this?

tostring(hash("test")) == "hash: [test]"
2 Likes

Note that this only determines if it’s a debug build or not. Not that it’s running from the editor.

I’m not sure I understand this?

In debug mode, you can print out a hash value and see the original string (i.e. “hash: [test]” in this case).

Whereas in release mode, the debug strings are turned off.

3 Likes

Another option might be to use sys.get_application_path(). When running from the editor, at least on macOS, you’ll get the path to the Defold.app.

local function is_editor()
	local path = sys.get_application_path()
	return path:match(".*%/Defold%.app") ~= nil -- macOS
		or path:match(".*%/Defold%.exe") ~= nil -- Windows
		or path:match(".*%/Defold") ~= nil -- Linux
end
2 Likes