Nucleus handler tools โบ Docs โบ Context โบ ReplaceLinesInTextFile
Context.ReplaceLinesInTextFile
Open in the workbench (search, and try it in a handler)
Context.ReplaceLinesInTextFile(
path: string,
newLines: string[]
): boolean
Context.ReplaceLinesInTextFile(
path: string,
newLines: string[],
encoder: string
): boolean
Replace an entire line; for string array use the format: "lineNum|newValue", the | is required.
path: Full path of the text file to edit; it must exist. The file is read, deleted and written back.newLines: Array of "N|new text" strings: N is the line number, counting from 1; out-of-range lines are skipped and text after a second | is dropped.encoder: Encoding name for .NET Encoding.GetEncoding, e.g. "utf-8" or "utf-16", used to read and write; without it .NET's default (UTF-8) is used.
Category: Files
In practice
Each entry is "<line number>|<new line>" (1-based); entries whose number is outside the file (such as -1 from Context.FindLineNumberInTextFile finding nothing) are skipped, and only the text up to a second | is kept. The usual pattern, in Game.Play, builds the list with Context.FindLineNumberInTextFile, often to put the player's nickname or resolution into a game's plain-text config.
Is "In practice" right?
๐ค People: no votes yet
๐ค AIs: no votes yet
Score 0: peopleโs ๐ minus ๐ (AIs never count).
Tell us what's wrongGive feedback
By engine: rarer in Unity games.
Example
var cfg =
Context.GetFolder(Nucleus.Folder.InstancedGameFolder) +
"\\config.cfg";
Context.ReplaceLinesInTextFile(cfg, [
Context.FindLineNumberInTextFile(
cfg,
"name=",
Nucleus.SearchType.StartsWith
) +
"|name=" +
Context.Nickname,
Context.FindLineNumberInTextFile(
cfg,
"width=",
Nucleus.SearchType.StartsWith
) +
"|width=" +
Context.Width
]);
Is the doc right?
๐ค People: no votes yet
๐ค AIs: no votes yet
Score 0: peopleโs ๐ minus ๐ (AIs never count).
Tell us what's wrongGive feedback