Page 1 of 1
How I change directory with love.filesystem
Posted: Fri May 12, 2023 1:29 pm
by Catzonic
Hi there everyone I was new I LOVE I was trying to a filesystem that write file in Lua for entities and ingame propertys I want that I save my file in different dictionary not on main LOVE dictionary
Re: How I change directory with love.filesystem
Posted: Sat May 13, 2023 9:39 am
by Sasha264
Good day and welcome!
Sadly, Love2d has some unpleasant restrictions about working with files aside from default savegame directory.
This is caused by some cross-platform features and some security reasons.
With
love.filesystem you can only change the name of save directory with
love.filesystem.setIdentity but not the full path to it:
Code: Select all
love.filesystem.setIdentity("my_lovely_game_saves")
But there are few ways to get around it:
- You can use Lua functions to write to a file, not the Love ones. But keep in mind that Lua's functions for working with files are not the best and reliable ones.
Code: Select all
local file = io.open("D:\\example.txt", "w+")
file:write("Hello from Lua!")
file:close()
- You can write a file in Love directory and then move it anywhere you want with os Lua functions.
Code: Select all
love.filesystem.write("example.txt", "Hello from Love!")
local src_path = love.filesystem.getSaveDirectory() .. "/example.txt"
local dst_path = "D:\\example.txt"
os.rename(src_path, dst_path)
- You can do something in C (or potentially some other language) using Lua JIT FFI. Maybe (not sure this will work anywhere, but works for me):
Code: Select all
local ffi = require("ffi")
ffi.cdef([[
typedef struct FILE FILE;
FILE* fopen(const char *filename, const char *mode);
int fprintf(FILE* stream, const char* format, ...);
int fclose(FILE* stream);
]])
local file = ffi.C.fopen("D:\\example.txt", "w")
ffi.C.fprintf(file, "Hello from C FFI !")
ffi.C.fclose(file)
- You can use NativeFS library that mostly replicates Love's filesystem, but without restrictions to file paths:https://github.com/EngineerSmith/nativefs
Re: How I change directory with love.filesystem
Posted: Sat May 13, 2023 3:20 pm
by zorg
You can also wait for 12.0 to relax the restrictions a bit.