Is there a way to delete strings in a text file with the File class?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JTJonny
:warning: Old Version Published before Godot 3 was released.

It seems you can only store with the File class.

:bust_in_silhouette: Reply From: Akien

Sure. Did you check the File class reference?

There are many ways to do what you want. A simple one would be to get the File’s contents as a String with File.get_as_text(), then modify the String with the usual string manipulation methods, and overwrite the file with the new String.

Or you could parse the file line by line and write it into a new file with the modifications you want.

The only way I found to overwrite things is to use Directory.remove(), then use File again. The problem is I’m working with the engine.cfg file, so I’m scared if something went wrong the player might not be able to open the game again. Is there some other safer way to write over files, or am I being crazy? (I’m using File on a .cfg, because I’m basically replacing the whole input section)

JTJonny | 2016-07-27 17:27

You should probably use the ConfigFile API for that: ConfigFile — Godot Engine (latest) documentation in English

Akien | 2016-07-27 17:35