using gsub() on a filename and it does not open
Posted: Fri Jun 12, 2020 1:05 pm
In my program I have an edit background feature, with the ability for a user to insert a png from their computer as the background, I've added the ability to paste text in, however because windows uses "\" for paths and love2d requires "/" for paths I have had to use gsub() to change between them. However when this is done the file path fails to load, whereas if i manually type the file path with / instead of \ it works. I'm unsure if this is an issue with gsub() or the code used to open the file:
if key == "return" then
print(InsertImageText)
InsertImageText = (InsertImageText):gsub("\\", "/")
print(InsertImageText)
local file = io.open(InsertImageText , "rb")
if file then
local contents = file:read("*all")
local data = love.filesystem.newFileData( contents, "img.png", "background.png" )
local imgdata = love.image.newImageData( data )
file:close()
BackgroundImage = love.graphics.newImage( imgdata )
else
Error("Failed to find specified file")
end
end
even running an if statement to check if the covnerted string is the same as one i could manually type says they are equal, despite the code being unable to open the file path
if key == "return" then
print(InsertImageText)
InsertImageText = (InsertImageText):gsub("\\", "/")
print(InsertImageText)
local file = io.open(InsertImageText , "rb")
if file then
local contents = file:read("*all")
local data = love.filesystem.newFileData( contents, "img.png", "background.png" )
local imgdata = love.image.newImageData( data )
file:close()
BackgroundImage = love.graphics.newImage( imgdata )
else
Error("Failed to find specified file")
end
end
even running an if statement to check if the covnerted string is the same as one i could manually type says they are equal, despite the code being unable to open the file path