Thank you to everyone who helped out! This code (provided you have a list of words you want filtered) checks a user's inputted name/word for any rude words that are in a .txt list. I hope this is able to help others as well in the future. ^_^
BTW, the .txt file of rude words (called rude_words.txt in the code below) should have each word on a separate line and all be in lowercase. Also, Ren'Py will look for the file in game/docs which is not a standard directory in a new Ren'Py game directory. Makes sure to create the file folder or change the path if you decide to use the code.
Code: Select all
# Before start label
init python:
import collections
def ContainsBadWord(name):
#Get list of bad words from text file, each word on a separate line
rude_words_file = [line.rstrip('\n') for line in open(renpy.loader.transfn('docs/rude_words.txt'), 'r')]
#Check if the name contains a bad word
for badWord in rude_words_file:
if (badWord in name):
return True
# After start label
label fname:
$ PL_fname = renpy.input("What is your first name?", default="Frisk", allow="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", length=10)
if not PL_fname:
$ PL_fname = "Frisk"
$ rude_words_file = [line.rstrip('\n') for line in open(renpy.loader.transfn('docs/rude_words.txt'), 'r')]
#if ( PL_fname.lower().strip() in rude_words_file ):
if (ContainsBadWord(PL_fname.lower().strip())):
jump frude
else:
jump fcont
label frude:
ed "Don't be rude."
jump fname
label fcont:
ed "Continue!"
Original Post:
So I'm working on an educational game for a local teacher and she wants to make sure the kids can't put in rude names (like swear words and other offensive terms). My first couple of tries resulted in Ren'Py crashes before it could finish compiling the scripts for play testing, but I've figured out a "working" version... sort of.
What I'd really like to do is be able to check an inputted name versus a master list that also uses wildcards (*rude* would also catch words like prude, rudes, etc.). I've looked into regular Python code, but most of it goes right over my head. I've included a sample of the "working" code I'm using at the moment, but I'm more than open (and hoping) for improvements and help.