Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Hobbes on Sun 04/11/2018 14:56:30

Title: SOLVED: Custom name. How to filter for bad words / empty string
Post by: Hobbes on Sun 04/11/2018 14:56:30
Slowly slowly working away on my prototype a bit more... I'm trying to add a character name to the creation screen.

I've got 2 questions. It's working fine, but right now the game will allow any name + empty string.

1. As a test I tried to get it to non parse "Shit" as a string. That works. However I don't really want to write 10000 if-else statements. Is there a way to parse all the swearwords in one if-statement?

2. Also, how would I parse an empty string and not accept it?

This is what I have so far:

Code (ags) Select

function Button1_OnClick(GUIControl *control, MouseButton button)
{

if (TextBox1.Text == "Shit") { Display("Try again... foulmouthed person!"); }

else {
player.Name = TextBox1.Text;
player.ChangeRoom(3, 536, 229, eDirectionLeft);
}
}
Title: Re: Character Creation - Custom name. How to filter for bad words / empty string?
Post by: Crimson Wizard on Sun 04/11/2018 15:10:47
Quote from: Hobbes on Sun 04/11/2018 14:56:30
1. As a test I tried to get it to non parse "Shit" as a string. That works. However I don't really want to write 10000 if-else statements. Is there a way to parse all the swearwords in one if-statement?

Technically there is, you can use OR operator like (TextBox1.Text == "Shit" || TextBox1.Text == "Crap"), but that will end up in a single giant "if" condition, which still may not be the optimal way code-wise.
Instead, you could define an array of words and iterate over it:

Code (ags) Select

int BadWordsCount;
String BadWords[];

void InitBadWords()
{
    BadWordsCount = 100;
    BadWords = new String[BadWordsCount];
    BadWords[0] = "Shit";
    BadWords[1] = "Crap";
    ...

    // You could even read this list of words from the file
}

function game_start()
{
   InitBadWords();
}


The bad words test:
Code (ags) Select

function Button1_OnClick(GUIControl *control, MouseButton button)
{
   for (int i = 0; i < BadWordsCount; i++)
   {
      if (TextBox1.Text.CompareTo(BadWords[i]) == 0)
      {
          Display("Try again... foulmouthed person!");
          return;
      }
   }

   // passed the test
}


Notice the use of "CompareTo" function, it deals with case-insensitive comparison, which means that it will detect "Shit", "SHit" and "SHIT" alltogether.

Quote from: Hobbes on Sun 04/11/2018 14:56:30
2. Also, how would I parse an empty string and not accept it?

There is String.IsNullOrEmpty() function that detects the zero-length string and null-pointers. But that won't detect a string full of whitespaces. So you perhaps need a more sophisticated test:

Code (ags) Select

bool IsNullOrWhitespace(String s)
{
    if (s == null)
       return true;
    for (int i = 0; i < s.Length; i++)
    {
        if (s.Chars[i] != ' ')
            return false; // found at least one non-whitespace character
    }
    return true;
}


Then -
Code (ags) Select

function Button1_OnClick(GUIControl *control, MouseButton button)
{
    if (IsNullOrWhitespace(TextBox1.Text))
    {
        Display("Type an actual name!");
        return;
    }
Title: Re: Character Creation - Custom name. How to filter for bad words / empty string?
Post by: Khris on Sun 04/11/2018 15:14:18
You could use the built-in text parser to do that. Add the first word to the list. Then keep right-clicking it and choose "add synonym". In your code, use

  Parser.ParseText(TextBox1.Text);
  if (Parser.Said("shit")) ...

All other swear words are synonyms, so this should match any word you added. And it even catches leading or trailing spaces, afaik.
Title: Re: Character Creation - Custom name. How to filter for bad words / empty string?
Post by: Crimson Wizard on Sun 04/11/2018 15:16:00
Oh, right, parser may be the good builtin solution for this.
Title: Re: Character Creation - Custom name. How to filter for bad words / empty string?
Post by: Hobbes on Sun 04/11/2018 15:28:49
Text parser is working fine, thanks for this. Together with the IsNullOrWhitespace code, this is now working flawlessly. Thank you both!!
Title: Re: SOLVED: Custom name. How to filter for bad words / empty string
Post by: Snarky on Sun 04/11/2018 15:40:13
So the first puzzle of the game is "find the bad word that the programmers forgot to add to the filter". That's a classic!
Title: Re: SOLVED: Custom name. How to filter for bad words / empty string
Post by: Click'd on Sun 04/11/2018 16:17:25
Quote from: Snarky on Sun 04/11/2018 15:40:13
So the first puzzle of the game is "find the bad word that the programmers forgot to add to the filter". That's a classic!
Another one is the "offensive string inside a perfectly harmless word".
https://en.wikipedia.org/wiki/Scunthorpe_problem
Title: Re: SOLVED: Custom name. How to filter for bad words / empty string
Post by: Hobbes on Sun 04/11/2018 16:18:12
(nod)

Looking forward to what creative things people can come up with. And what silly easter eggs I might put in.
Title: Re: SOLVED: Custom name. How to filter for bad words / empty string
Post by: Cassiebsg on Sun 04/11/2018 18:31:34
Uhm, that's how I ended up not being allowed to use my nick in a forum... cause it contained the word "ass"... (roll)
Title: Re: SOLVED: Custom name. How to filter for bad words / empty string
Post by: Hobbes on Sun 04/11/2018 21:13:11
Even though the BSG part should've overruled everything in my opinion. That should be an automatic pass. So say we all!
Title: Re: SOLVED: Custom name. How to filter for bad words / empty string
Post by: Cassiebsg on Mon 05/11/2018 14:34:49
Wait wait. You can't type "pass" either... (laugh)