How to have multiple folders of saved local data? [Cocos 3.8.2]

Is it possible to have folders and subfolders using cc.sys.localStorage? Like this:

user storage
    folder 1
        subfolder 1
        subfolder 2
    folder 2

I’m making a game where players create levels and save them to local storage and this functionality would be very useful.

You can use this.

    private saveData(){
        if(sys.isNative){
            //create folder and file just working on native platform.
            let f = native.fileUtils;
            let base = f.getWritablePath();
            let folder = base + "folder/subfolder/";
            let success = f.createDirectory(folder);
            if(success){
                f.writeStringToFile("game data", folder + "gamedata");
            }
        }else{
            //web platform not support this way.
        }
    }

Thank you! Does this work with the “Preview in Editor (Beta)” option? And is there some way to do this on the browser?

It not work with Editor. it works with “Preview in Simulator”. The writeablepath in my computer is “C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\native\simulator\Release”.

Browser not allow create folder and file.If you want save data in browser, you should save them as string. like this.

function saveData() {
    let gamedata = {
        folder1: {
            subfolder1: "gamedata1",
            subfolder2: "gamedata2",
        },
        folder2: {
            subfolder3: "gamedata3",
        }
    }

    localStorage.setItem("GAMEDATA", JSON.stringify(gamedata));
}

function readData(){
    let gameData;
    try {
        gameData = JSON.parse(localStorage.getItem("GAMEDATA"));
    } catch (error) {
        gameData = {} //default data
    }

    let data1 = gameData["folder1"]["subfolder1"];
}
1 Like