How I change directory with love.filesystem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Catzonic
Prole
Posts: 5
Joined: Sat Feb 18, 2023 5:53 am
Contact:

How I change directory with love.filesystem

Post 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
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: How I change directory with love.filesystem

Post by Sasha264 »

Good day and welcome! :3

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
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How I change directory with love.filesystem

Post by zorg »

You can also wait for 12.0 to relax the restrictions a bit.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests