How do I translate a word into L337 form?

I’m trying to making a game where I can type a word into an inputfield and when the L337 toggle is set on, the L337 version of that word comes out the output text, which is pretty much replacing some letters with numbers and other letters. It basically works, but I can’t get the result to be encrypted into L337 form. It just comes out the same way it came in, whether the toggle is on or off. Could someone help me please?

public class Encryption : MonoBehaviour
{
InputField input;
public Text output;
string inputText;

public Toggle L337Toggle;

 void Start()
{
   L337Toggle.isOn = false;


}
private void Update()
{
   inputText = input.text;
   output.text = inputText;

   var textEncryption = new TextEncryption(inputText);
   var L337Encryption = new L337Encryption(textEncryption);

   if (Input.GetKeyDown("enter"))
   {
       if (L337Toggle.isOn == true)
       {
           string result = L337Encryption.Encrypt();
       }
   }
}


public interface IEncryption
{
   string Encrypt();
}

public class TextEncryption : IEncryption
{
   private string originalString;

   public TextEncryption(string original)
   {
       originalString = original;
   }
   public string Encrypt()
   {
       Debug.Log("Encrypting Text");
       return originalString;
   }
}

public class L337Encryption : IEncryption
{
   private IEncryption _encryption;

   public L337Encryption(IEncryption encryption)
   {
       _encryption = encryption;
   }
   public string Encrypt()
   {
       Debug.Log("Encrypting L337 Text");
       string result = _encryption.Encrypt();
       result = result.Replace('a', '4').Replace('b', '8').Replace('e', '3').Replace('g', '6').Replace('h', '4').Replace('l', '1')
           .Replace('0', '0').Replace('q', '9').Replace('s', '5').Replace('t', '7');

       return result;
   }
}
}

I’ll say, don’t do this in update. Inputfields have events to handle when text changes at which point you could convert it either on a letter by letter or when it ends. Otherwise, I don’t see where you’re assigning the results from the Encrypt back to any UI object. I see you’re checking if the toggle is on, then getting the string value and assigning it to result, but then what? How are you outputting it?

Also, I just feel like your script is over complicating things. Use the inputfield event system. And just have a simple method. You can use on Value changed event for each time a character is input. Or on end edit for when they finish typing in and do the check at the end. Trigger the method to check for the toggle and either assign the input text into your output text object or to run it through your replace call and output the result to the output text object.

You’re not doing anything with your result on line 28. Change it to this:

output.text = L337Encryption.Encrypt();

Agreed about avoiding Update. Should be avoid both for creating those new encryptions and for setting the text. Only do those things when (you’re called upon) to do 'em, so to speak :slight_smile: