0 votes

I'm currently creating a game that contains a chat feature. I'm working on the swear filter right now. I'm using replace() and right now it looks like: text.replace("naughty word", "* * *") but to get all the words that would be a bunch of lines of code. So I was wondering if I could store all the bad words into a variable, and then it would look something like:

var swear_words = (however you create a list)

text.replace(swear_words, "* * *")

in Engine by (15 points)

This always makes me laugh. You don't want swear words in your game so you ..... Drumroll please ........ Put all known swear words under the sun in your game. Lol.

So a tip, replace the word(s) with an empty string because
A ***ing sentence like this is pretty suggestive
And another @$#& one like this ain't any better

3 Answers

+2 votes
Best answer

The provided answers will work were you loop through the array and replace the text for each swear word.

You could also use a regex to replace the swear words in one replacement.

var r = RegEx.new()
var string = "This is a text full of swear words like swear1 or swear2 and even swear3. It even gets repeated swear1, swear2"
var replacedstring = r.sub(string, "* * *", true)

This prints

This is a text full of swear words like * * * or * * * and even * * *. It even gets repeated * * *, * * *
by (114 points)
selected by
0 votes

You're looking for an Array (or PoolStringArray). You will need to loop through the array and check every word to replace, however.

by (7,148 points)
–1 vote

You need to use an array and a for loop as follows :

text = "Your string with swear words" 
var swear_words = ["word_1","word_2","word_3"]
for word in swear_words:
    text.replace(word, "****")

Replace the contents of text and swear_words with your text and swear words and it should work.

by (1,051 points)
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.