Io.open() with address

Hi!

I am using

	file = io.open("//Users//joshrackstraw//test.lua", "r")

but it causes the game to freeze. Taking the above code out means everything works fine. I know the path is correct because

print(os.getenv("HOME"))

gives me /Users/joshrackstraw. What am I doing wrong? I’m on MacOS and the goal here is to create a file in a common location that can be opened by two different apps created in defold.

and just to be clear, file = io.open(“test.lua”, “r”) works as it should

Perhaps lua get’s confused by the double slashes? Have you tried " /Users/joshrackstraw/test.lua" ?

Yep- I’ve tried a few formatting things with no luck. I don’t even get an error message!

Does it work with a path from sys.get_save_file()?

And, what is the current dir for the process? E.g. print before the “io.open()”:

print(os.getenv(‘PWD’))

And, does it freeze right on that line?
Or can you for instance print a message after the io.open()?

I’m unable to reproduce any freeze. I tried the following:

function init(self)
	local f, err = io.open("/Users/bjornritzl/projects/monarch/README.md", "r")
	print(f, err)
end

Console shows:

DEBUG:SCRIPT: file (0x7fff92ab1020)	nil

Can you share more of your code? Seems like there’s something else affecting it.

Have you tried loading the file from ~/Library/Application Support? I’m in Windows land so for me it’s %Appdata%. If you use something like

local path = sys.get_save_file(string.gsub(sys.get_config('project.title'), '%s+', ''), 'filename')

And then save off some data to that path, then you can replace that file with your own data and then load it from a place where permissions hopefully won’t be an issue.

okay, the error appears when i do

file = io.open(" /Users/joshrackstraw/test.lua", "w")
--io.input(file)
print(io.read())
--io.close(file)

and i tried

file = io.open(" /Users/joshrackstraw/test.lua", "w")
--io.input(file)

--io.close(file)
local f, err = print(io.read())
print(f, err)

and it doesn’t give print anything, just freezes the app

That’s a space at the beginning of the path. And you are reading from a file you opened in write mode.

2 Likes