0 votes

I created some .dat and .json files in the user:// folder . But it wasn't deleted while deleting the game. How to make deleting all the created files during the game while deleting game possible

in Engine by (34 points)

1 Answer

0 votes

Hi,
Not quite sure what you mean by it wasn't deleted while deleting the game but here's some code that will create 10 .dat & .json files and then delete all files by extension:

extends Node2D

func _ready() -> void:
    var save_game 
    for i in range(10):
        save_game = File.new()
        save_game.open("user://savegame"+str(i)+".json", File.WRITE)
        save_game.store_line("This is a test")
        save_game.close()

    for i in range(10):
        save_game = File.new()
        save_game.open("user://savegame"+str(i)+".dat", File.WRITE)
        save_game.store_line("This is a test")
        save_game.close()   

    delete_file_with_extension("dat")
    delete_file_with_extension("json")


func delete_file_with_extension(ext):
    var dir = Directory.new()
    dir.open("user://")
    dir.list_dir_begin()
    while true:
        var file = dir.get_next()
        if file == "":
            break
        elif not file.begins_with(".") and file.right((file.length()-ext.length())) == ext:
            dir.remove(file)
    dir.list_dir_end()
by (1,861 points)

I mean after exporting game i play it and do some saves after that i delete my game and export it again . The problem is that save files still exists in the user folder. They wasn't deleted with the game.

Right OK.
I don't know where data files are saved when exported, but I know they are defined here when in the editor. So if the user files are in a different directory, then they won't get deleted.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.