I want to have an option to censor some events and words if the player wishes to do so. Would it be possible to use a common event to replace every instance of an uttered word with something else? Something like... if switch 20 is active, replace every instance of (word) with (another word)?
Is there a way to replace every instance of a word being said with another word?
RPG Maker MZ SupportNo. You would have to either:
- have two versions of every Show Text that contains the things you want to change, in Conditional Branches to control which one is shown depending on the state of a switch,
or
- Use something like Hakuen's Global Text which allows you to embed JavaScript into your Show Text. Then you could have just one Show Text command and use a JS conditional to change which word is printed.
- have two versions of every Show Text that contains the things you want to change, in Conditional Branches to control which one is shown depending on the state of a switch,
or
- Use something like Hakuen's Global Text which allows you to embed JavaScript into your Show Text. Then you could have just one Show Text command and use a JS conditional to change which word is printed.
It would require going back and adjusting any existing message where the words show up, but another option is to store the words in variables. So if you stored ”spider” in variable 5 and wanted to change it to “bug” if switch 20 was On you could do that with a common event at the start of the game. Then you’d use /v[5] (not at my computer so I can’t double-check that) in the messages instead of the actual word and the word you want should appear.
Not a common event, but this is doable with a fairly simple plugin. I don't have a way to test it rn, but it should do the trick:
This will replace every instance of "word" with "another word" if switch 20 is true. You can copy that if statement as many times as you'd like and change the switch number, the starting and the resulting word.
Note that the regex will only work if the word is not touching any other letters - again I'm assuming this is what you probably want; it will replace "word!" with "another word!", but leave "wordle" alone. It will also match the characters exactly, so it won't change "Word" either.
Edit: Actually, it will probably be more reliable to alias drawTextEx. Also fixed the regex. Twice xD
Edit2: I've now been able to test it, so this version should work fine.
JavaScript:
var _Game_Message_add = Game_Message.prototype.add;
Game_Message.prototype.add = function (text) {
let newText = text;
if ($gameSwitches.value(20)) {
newText = newText.replace(/(?<!\w)word(?!\w)/g, "another word");
}
_Game_Message_add.call(this, newText);
};
This will replace every instance of "word" with "another word" if switch 20 is true. You can copy that if statement as many times as you'd like and change the switch number, the starting and the resulting word.
Note that the regex will only work if the word is not touching any other letters - again I'm assuming this is what you probably want; it will replace "word!" with "another word!", but leave "wordle" alone. It will also match the characters exactly, so it won't change "Word" either.
Edit: Actually, it will probably be more reliable to alias drawTextEx. Also fixed the regex. Twice xD
Edit2: I've now been able to test it, so this version should work fine.
There are some errors in your regex lookarounds. This should do it:
JavaScript:
/(?<!\w)butt(?!\w)/g
I should stop using this forum on my phone xD
You are correct, the = signs certainly don't belong in a negative lookaround. Thanks!
You also wanna make sure that the less-than character is in front of the exclamation mark.
I think this is going to be a more trustworthy method.
You'll probably want a message plugin with word-wrap or whatever to autofit text to textboxes.
Unless you're replacing a 4 letter word with another 4 letter word.
Unless you're replacing a 4 letter word with another 4 letter word.
I've now been able to actually test it, so the version currently in my previous post should work fine.
Fun fact: did you guys know that Window_Message doesn't actually use either Window_Base's drawText or drawTextEx, nor does it overwrite those, but simply draws directly to its contents? I sure didn't xD
It's worth pointing out that this approach should work perfectly fine if you're trying to replace swear words with stars or versions that are mostly stars. As @hiddenone, @pawsplay and @coyotecraft pointed out, if you're trying to replace them with other words this will be a bit more involved. There is no single word that can replace f*** in every context it's used in, so you'll have to deal with replacements on a case-by-case basis, which is easier done by just using variables in messages (and minding the length of both possibilities). It might also be worth looking into translation plugins, since they're made for exactly doing this.
Fun fact: did you guys know that Window_Message doesn't actually use either Window_Base's drawText or drawTextEx, nor does it overwrite those, but simply draws directly to its contents? I sure didn't xD
Also fixed that. Thanks!
It's worth pointing out that this approach should work perfectly fine if you're trying to replace swear words with stars or versions that are mostly stars. As @hiddenone, @pawsplay and @coyotecraft pointed out, if you're trying to replace them with other words this will be a bit more involved. There is no single word that can replace f*** in every context it's used in, so you'll have to deal with replacements on a case-by-case basis, which is easier done by just using variables in messages (and minding the length of both possibilities). It might also be worth looking into translation plugins, since they're made for exactly doing this.
You can actually do this without a plugin with a bit of clever scripting in your common event:
This would replace any instance of "work" in a message in the event with "taxes". The next step then is obviously to add a condition to that.
You're likely still better off using a plugin, I just wanted to note that technically you can do it without one. 🙂
JavaScript:
let cmdIdx = 0;
for (const cmd of $gameMap._interpreter._list) {
if (cmd.code === 401) {
let lineIdx = 0;
for (let line of cmd.parameters) {
$gameMap._interpreter._list[cmdIdx].parameters[lineIdx] = line.replace("work", "taxes");
lineIdx++;
}
}
cmdIdx++;
}
This would replace any instance of "work" in a message in the event with "taxes". The next step then is obviously to add a condition to that.
You're likely still better off using a plugin, I just wanted to note that technically you can do it without one. 🙂
Maybe this plugin?
forums.rpgmakerweb.com
@Trihan - a caveat: the command list is passed by reference from
codapill_CensorText [MV + MZ]
Censor Text v1.0 codapill Introduction Simple plugin that allows you to censor certain words for whatever reason in your game. Those selected words will instead display random symbols, or a string of your choice. Features Provide a list of terms in the plugin's parameters and those words will...
forums.rpgmakerweb.com
@Trihan - a caveat: the command list is passed by reference from
$dataMap (or $dataCommonEvents) rather than cloned, so editing a (sub-)property of that array will mutate the source object until it is reloaded. Generally not a big deal for map events since $dataMap is reloaded on map change, but I thought it was worth mentioning (it's caught me out before).A good point to raise, but I'm not sure in this case it does. When I was testing it, the word only changed when I ran that script; if I interacted with the event afterwards without it, the word was unchanged.
Replacing words even without white space could up doing a lot of "dawizard" to your project.
That's why it looks for letters around it - it will replace "word", but won't replace anything inside "wordle". If specifically whitespace was required, it wouldn't replace words that are next to punctuation, colour codes etc.
Original thread: https://forums.rpgmakerweb.com/threads/is-there-a-way-to-replace-every-instance-of-a-word-being-said-with-another-word.161244/