Can't get string insert() and replace() to work

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Macarthius

I’m trying to create a game similar to a MUD in which the player types a command and then their action appears in a log with the result of that action. I have the commands working but not the log.

I figured I could use string insert() to add each action as a new line of a read only TextEdit node. After playing around with it I can’t seem to get insert() or replace() to do anything. I get no errors but it still outputs the original string.

I suppose in my case I could use an array to store the strings and a for loop to concatenate all of them but I wanted to submit this anyways in case I’m doing something wrong or if it’s a bug. I’m using the latest stable version of Godot (via steam).

func _ready():
	var text = " sea if this works."
	text.insert(0, "Let's")
	text.replace("sea", "see")

Output: sea if this works.
:bust_in_silhouette: Reply From: wombatstampede

My guess is that the insert and replace functions return the altered string but don’t alter the original string.

Probably you could do
text = text.insert(0, "Let's").replace("sea", "see")