Search Unity

How do I translate a word into L337 form?

Discussion in 'Scripting' started by KDamon, Oct 19, 2017.

  1. KDamon

    KDamon

    Joined:
    Sep 21, 2017
    Posts:
    33
    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?

    Code (CSharp):
    1.  
    2. public class Encryption : MonoBehaviour
    3. {
    4. InputField input;
    5. public Text output;
    6. string inputText;
    7.  
    8. public Toggle L337Toggle;
    9.  
    10.  void Start()
    11. {
    12.    L337Toggle.isOn = false;
    13.  
    14.  
    15. }
    16. private void Update()
    17. {
    18.    inputText = input.text;
    19.    output.text = inputText;
    20.  
    21.    var textEncryption = new TextEncryption(inputText);
    22.    var L337Encryption = new L337Encryption(textEncryption);
    23.  
    24.    if (Input.GetKeyDown("enter"))
    25.    {
    26.        if (L337Toggle.isOn == true)
    27.        {
    28.            string result = L337Encryption.Encrypt();
    29.        }
    30.    }
    31. }
    32.  
    33.  
    34. public interface IEncryption
    35. {
    36.    string Encrypt();
    37. }
    38.  
    39. public class TextEncryption : IEncryption
    40. {
    41.    private string originalString;
    42.  
    43.    public TextEncryption(string original)
    44.    {
    45.        originalString = original;
    46.    }
    47.    public string Encrypt()
    48.    {
    49.        Debug.Log("Encrypting Text");
    50.        return originalString;
    51.    }
    52. }
    53.  
    54. public class L337Encryption : IEncryption
    55. {
    56.    private IEncryption _encryption;
    57.  
    58.    public L337Encryption(IEncryption encryption)
    59.    {
    60.        _encryption = encryption;
    61.    }
    62.    public string Encrypt()
    63.    {
    64.        Debug.Log("Encrypting L337 Text");
    65.        string result = _encryption.Encrypt();
    66.        result = result.Replace('a', '4').Replace('b', '8').Replace('e', '3').Replace('g', '6').Replace('h', '4').Replace('l', '1')
    67.            .Replace('0', '0').Replace('q', '9').Replace('s', '5').Replace('t', '7');
    68.  
    69.        return result;
    70.    }
    71. }
    72. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    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.
     
    Last edited: Oct 19, 2017
  3. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    You're not doing anything with your result on line 28. Change it to this:
    Code (csharp):
    1.  
    2. output.text = L337Encryption.Encrypt();
    3.  
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :)