Search Unity

Is this a normal way to code?

Discussion in 'Scripting' started by San_Holo, Apr 5, 2015.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Hi...

    I'm fairly new to all this and i was wondering if any of you just.. plough the road so to speak with spaghetti-coding just to get the script working, then spend the time whittling away, might be just me... but I had this idea, I quickly thunk it all up, put it together till it was all working then I said to myself oh my lord I have to clean up that code, so it does the same thing just better & faster and in less lines... I must do this more, it's called development doh I know... as they say there's always version 1.1...

    Below is the bulky clobber then the next one is the tidy version, which thankfully looks much nicer.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Text;
    5.  
    6. /**
    7. *  GALAP - v 1.0a
    8. *  DEVELOPER NOTES : San Holo 05/04/2015
    9. *  ==========================================
    10. *  Note: Display high-scores as teletype message with color rotation
    11. *  TO-DO : everything
    12. *
    13. **/
    14.  
    15. public class HighscoreDisplay : MonoBehaviour
    16. {
    17.         public TextMesh HighScoresLine1Mesh = null;
    18.         public TextMesh HighScoresLine2Mesh = null;
    19.         public TextMesh HighScoresLine3Mesh = null;
    20.         public TextMesh HighScoresLine4Mesh = null;
    21.         public TextMesh HighScoresLine5Mesh = null;
    22.         public TextMesh HighScoresLine6Mesh = null;
    23.         public TextMesh HighScoresLine7Mesh = null;
    24.         public TextMesh HighScoresLine8Mesh = null;
    25.         public TextMesh HighScoresLine9Mesh = null;
    26.         public TextMesh HighScoresLine10Mesh = null;
    27.         public float DisplayTime = 20.0f;
    28.         private float _timer = 0.0f;
    29.         private string text, message, text2, message2, text3, message3, text4, message4, text5, message5, text6, message6, text7, message7, text8, message8, text9, message9, text10, message10, text11, message11, text12, message12;
    30.         public float letterPause = 0.01f;
    31.         public bool FormatTopScore = false; // used to change 1st place for name entry screen
    32.         private GameObject topplayerstext, galaxypilotstext;
    33.         private TextMesh pilotsLeader, topLeader;
    34.  
    35.         void Start ()
    36.         {
    37.                 _timer = 0.0f;
    38.                 if (Application.loadedLevelName == "MainMenu") {
    39.                         topplayerstext = GameObject.Find ("Top_Players");
    40.                         galaxypilotstext = GameObject.Find ("Pilots");
    41.                 }
    42.         }
    43.  
    44.         void OnEnable ()
    45.         {
    46.                 GameManager gameManager = GameManager.instance;
    47.                 if (gameManager != null) {
    48.                         List<HighScoreEntry> highScores = gameManager.GetHighScores ();
    49.                         if (highScores != null) {
    50.  
    51.                                 // Assign some variables
    52.                                 StringBuilder HighScoresLine1 = new StringBuilder ();
    53.                                 StringBuilder HighScoresLine2 = new StringBuilder ();
    54.                                 StringBuilder HighScoresLine3 = new StringBuilder ();
    55.                                 StringBuilder HighScoresLine4 = new StringBuilder ();
    56.                                 StringBuilder HighScoresLine5 = new StringBuilder ();
    57.                                 StringBuilder HighScoresLine6 = new StringBuilder ();
    58.                                 StringBuilder HighScoresLine7 = new StringBuilder ();
    59.                                 StringBuilder HighScoresLine8 = new StringBuilder ();
    60.                                 StringBuilder HighScoresLine9 = new StringBuilder ();
    61.                                 StringBuilder HighScoresLine10 = new StringBuilder ();
    62.                              
    63.                                 // Get the names & scores one by one
    64.                                 HighScoreEntry hse = highScores [0]; // Top Score!
    65.                                 if (hse != null && hse.Name != null) {
    66.                                         if (FormatTopScore) {
    67.                                                 HighScoresLine1.Append ("1st - ").Append (hse.Name).Append (" > ").Append (hse.Score);
    68.                                         } else {
    69.                                                 HighScoresLine1.Append ("* 1ST : ").Append (hse.Name).Append (" - High Score : ").Append (hse.Score).Append (" *");
    70.                                         }
    71.                                 }
    72.  
    73.                                 HighScoreEntry hse2 = highScores [1];
    74.                                 if (hse2 != null && hse2.Name != null) {
    75.                                         HighScoresLine2.Append ("2nd - ").Append (hse2.Name).Append (" > ").Append (hse2.Score);
    76.                                 }
    77.  
    78.                                 HighScoreEntry hse3 = highScores [2];
    79.                                 if (hse3 != null && hse3.Name != null) {
    80.                                         HighScoresLine3.Append ("3rd - ").Append (hse3.Name).Append (" > ").Append (hse3.Score);
    81.                                 }
    82.  
    83.                                 HighScoreEntry hse4 = highScores [3];
    84.                                 if (hse4 != null && hse4.Name != null) {
    85.                                         HighScoresLine4.Append ("4th - ").Append (hse4.Name).Append (" > ").Append (hse4.Score);
    86.                                 }
    87.  
    88.                                 HighScoreEntry hse5 = highScores [4];
    89.                                 if (hse5 != null && hse5.Name != null) {
    90.                                         HighScoresLine5.Append ("5th - ").Append (hse5.Name).Append (" > ").Append (hse5.Score);
    91.                                 }
    92.  
    93.                                 HighScoreEntry hse6 = highScores [5];
    94.                                 if (hse6 != null && hse6.Name != null) {
    95.                                         HighScoresLine6.Append ("6th - ").Append (hse6.Name).Append (" > ").Append (hse6.Score);
    96.                                 }
    97.  
    98.                                 HighScoreEntry hse7 = highScores [6];
    99.                                 if (hse7 != null && hse7.Name != null) {
    100.                                         HighScoresLine7.Append ("7th - ").Append (hse7.Name).Append (" > ").Append (hse7.Score);
    101.                                 }
    102.  
    103.                                 HighScoreEntry hse8 = highScores [7];
    104.                                 if (hse8 != null && hse8.Name != null) {
    105.                                         HighScoresLine8.Append ("8th - ").Append (hse8.Name).Append (" > ").Append (hse8.Score);
    106.                                 }
    107.  
    108.                                 HighScoreEntry hse9 = highScores [8];
    109.                                 if (hse9 != null && hse9.Name != null) {
    110.                                         HighScoresLine9.Append ("9th - ").Append (hse9.Name).Append (" > ").Append (hse9.Score);
    111.                                 }
    112.  
    113.                                 HighScoreEntry hse10 = highScores [9];
    114.                                 if (hse10 != null && hse10.Name != null) {
    115.                                         HighScoresLine10.Append ("10th - ").Append (hse10.Name).Append (" > ").Append (hse10.Score);
    116.                                 }
    117.  
    118.                                 // Start the coroutine chain-reaction
    119.                                 if (HighScoresLine1Mesh != null) {
    120.                                         text = "";
    121.                                         message = HighScoresLine1.ToString ();
    122.                                         StartCoroutine (PauseFirstScore ());
    123.                                 }
    124.                                 if (HighScoresLine2Mesh != null) {
    125.                                         text2 = "";
    126.                                         message2 = HighScoresLine2.ToString ();
    127.                                 }
    128.                                 if (HighScoresLine3Mesh != null) {
    129.                                         text3 = "";
    130.                                         message3 = HighScoresLine3.ToString ();
    131.                                 }
    132.                                 if (HighScoresLine4Mesh != null) {
    133.                                         text4 = "";
    134.                                         message4 = HighScoresLine4.ToString ();
    135.                                 }
    136.                                 if (HighScoresLine5Mesh != null) {
    137.                                         text5 = "";
    138.                                         message5 = HighScoresLine5.ToString ();
    139.                                 }
    140.                                 if (HighScoresLine6Mesh != null) {
    141.                                         text6 = "";
    142.                                         message6 = HighScoresLine6.ToString ();
    143.                                 }
    144.                                 if (HighScoresLine7Mesh != null) {
    145.                                         text7 = "";
    146.                                         message7 = HighScoresLine7.ToString ();
    147.                                 }
    148.                                 if (HighScoresLine8Mesh != null) {
    149.                                         text8 = "";
    150.                                         message8 = HighScoresLine8.ToString ();
    151.                                 }
    152.                                 if (HighScoresLine9Mesh != null) {
    153.                                         text9 = "";
    154.                                         message9 = HighScoresLine9.ToString ();
    155.                                 }
    156.                                 if (HighScoresLine10Mesh != null) {
    157.                                         text10 = "";
    158.                                         message10 = HighScoresLine10.ToString ();
    159.                                 }
    160.                         }
    161.                 }
    162.         }
    163.  
    164.         void Update ()
    165.         {
    166.                 _timer += Time.deltaTime;
    167.                 if (SceneManager_MainMenu.instance != null) {
    168.                         if (_timer > DisplayTime) {
    169.                                 // clear & reset
    170.                                 _timer = 0.0f;
    171.                                 text = "";
    172.                                 HighScoresLine1Mesh.text = text;
    173.                                 text2 = "";
    174.                                 HighScoresLine2Mesh.text = text2;
    175.                                 text3 = "";
    176.                                 HighScoresLine3Mesh.text = text3;
    177.                                 text4 = "";
    178.                                 HighScoresLine4Mesh.text = text4;
    179.                                 text5 = "";
    180.                                 HighScoresLine5Mesh.text = text5;
    181.                                 text6 = "";
    182.                                 HighScoresLine6Mesh.text = text6;
    183.                                 text7 = "";
    184.                                 HighScoresLine7Mesh.text = text7;
    185.                                 text8 = "";
    186.                                 HighScoresLine8Mesh.text = text8;
    187.                                 text9 = "";
    188.                                 HighScoresLine9Mesh.text = text9;
    189.                                 text10 = "";
    190.                                 HighScoresLine10Mesh.text = text10;
    191.                                 if (topplayerstext) {
    192.                                         topplayerstext.GetComponent<MeshRenderer> ().enabled = false;
    193.                                         text12 = "";
    194.                                         topLeader.text = text12;
    195.                                 }
    196.                                 if (galaxypilotstext) {
    197.                                         galaxypilotstext.GetComponent<MeshRenderer> ().enabled = false;
    198.                                         text11 = "";
    199.                                         pilotsLeader.text = text11;
    200.                                 }
    201.                                 SceneManager_MainMenu.instance.NextScreen ();
    202.                         }
    203.                 }
    204.         }
    205.  
    206.         IEnumerator TypeHighScoreLine1 ()
    207.         {
    208.                 foreach (char letter in message.ToCharArray()) {
    209.                         text += letter;
    210.                         HighScoresLine1Mesh.text = text;
    211.                         yield return 0;
    212.                         if (FormatTopScore) {
    213.                                 yield return new WaitForSeconds (letterPause);
    214.                         } else {
    215.                                 yield return new WaitForSeconds (0.01f); // Quicker delay setting for longer format
    216.                         }
    217.                 }
    218.                 StartCoroutine (TypeHighScoreLine2 ());
    219.         }
    220.  
    221.         IEnumerator TypeHighScoreLine2 ()
    222.         {
    223.                 foreach (char letter in message2.ToCharArray()) {
    224.                         text2 += letter;
    225.                         HighScoresLine2Mesh.text = text2;
    226.                         yield return 0;
    227.                         yield return new WaitForSeconds (letterPause);
    228.                 }
    229.                 StartCoroutine (TypeHighScoreLine3 ());
    230.         }
    231.  
    232.         IEnumerator TypeHighScoreLine3 ()
    233.         {
    234.                 foreach (char letter in message3.ToCharArray()) {
    235.                         text3 += letter;
    236.                         HighScoresLine3Mesh.text = text3;
    237.                         yield return 0;
    238.                         yield return new WaitForSeconds (letterPause);
    239.                 }
    240.                 StartCoroutine (TypeHighScoreLine4 ());
    241.         }
    242.  
    243.         IEnumerator TypeHighScoreLine4 ()
    244.         {
    245.                 foreach (char letter in message4.ToCharArray()) {
    246.                         text4 += letter;
    247.                         HighScoresLine4Mesh.text = text4;
    248.                         yield return 0;
    249.                         yield return new WaitForSeconds (letterPause);
    250.                 }
    251.                 StartCoroutine (TypeHighScoreLine5 ());
    252.         }
    253.  
    254.         IEnumerator TypeHighScoreLine5 ()
    255.         {
    256.                 foreach (char letter in message5.ToCharArray()) {
    257.                         text5 += letter;
    258.                         HighScoresLine5Mesh.text = text5;
    259.                         yield return 0;
    260.                         yield return new WaitForSeconds (letterPause);
    261.                 }
    262.                 StartCoroutine (TypeHighScoreLine6 ());
    263.         }
    264.  
    265.         IEnumerator TypeHighScoreLine6 ()
    266.         {
    267.                 foreach (char letter in message6.ToCharArray()) {
    268.                         text6 += letter;
    269.                         HighScoresLine6Mesh.text = text6;
    270.                         yield return 0;
    271.                         yield return new WaitForSeconds (letterPause);
    272.                 }
    273.                 StartCoroutine (TypeHighScoreLine7 ());
    274.         }
    275.  
    276.         IEnumerator TypeHighScoreLine7 ()
    277.         {
    278.                 foreach (char letter in message7.ToCharArray()) {
    279.                         text7 += letter;
    280.                         HighScoresLine7Mesh.text = text7;
    281.                         yield return 0;
    282.                         yield return new WaitForSeconds (letterPause);
    283.                 }
    284.                 StartCoroutine (TypeHighScoreLine8 ());
    285.         }
    286.  
    287.         IEnumerator TypeHighScoreLine8 ()
    288.         {
    289.                 foreach (char letter in message8.ToCharArray()) {
    290.                         text8 += letter;
    291.                         HighScoresLine8Mesh.text = text8;
    292.                         yield return 0;
    293.                         yield return new WaitForSeconds (letterPause);
    294.                 }
    295.                 StartCoroutine (TypeHighScoreLine9 ());
    296.         }
    297.  
    298.         IEnumerator TypeHighScoreLine9 ()
    299.         {
    300.                 foreach (char letter in message9.ToCharArray()) {
    301.                         text9 += letter;
    302.                         HighScoresLine9Mesh.text = text9;
    303.                         yield return 0;
    304.                         yield return new WaitForSeconds (letterPause);
    305.                 }
    306.                 StartCoroutine (TypeHighScoreLine10 ());
    307.         }
    308.  
    309.         IEnumerator TypeHighScoreLine10 ()
    310.         {
    311.                 foreach (char letter in message10.ToCharArray()) {
    312.                         text10 += letter;
    313.                         HighScoresLine10Mesh.text = text10;
    314.                         yield return 0;
    315.                         yield return new WaitForSeconds (letterPause);
    316.                 }
    317.         }
    318.  
    319.         IEnumerator TypePilotsLeaderText ()
    320.         {
    321.                 foreach (char letter in message11.ToCharArray()) {
    322.                         text11 += letter;
    323.                         pilotsLeader.text = text11;
    324.                         yield return 0;
    325.                         yield return new WaitForSeconds (letterPause);
    326.                 }
    327.         }
    328.  
    329.         IEnumerator TypeTopLeaderText ()
    330.         {
    331.                 foreach (char letter in message12.ToCharArray()) {
    332.                         text12 += letter;
    333.                         topLeader.text = text12;
    334.                         yield return 0;
    335.                         yield return new WaitForSeconds (letterPause * 4); // changed to slow typing effect
    336.                 }
    337.         }
    338.  
    339.         IEnumerator PauseFirstScore ()
    340.         {
    341.                 yield return new WaitForSeconds (1);
    342.                 if (topplayerstext) {
    343.                         topplayerstext.GetComponent<MeshRenderer> ().enabled = true;
    344.                         topLeader = topplayerstext.GetComponent<TextMesh> ();
    345.                         message12 = "*** BEST PLAYERS ***";
    346.                         StartCoroutine (TypeTopLeaderText ());
    347.                 }
    348.                 StartCoroutine (TypeHighScoreLine1 ());
    349.                 yield return new WaitForSeconds (1.2f);
    350.                 if (galaxypilotstext) {
    351.                         galaxypilotstext.GetComponent<MeshRenderer> ().enabled = true;
    352.                         pilotsLeader = galaxypilotstext.GetComponent<TextMesh> ();
    353.                         message11 = "-- GALAXY PILOTS --";
    354.                         StartCoroutine (TypePilotsLeaderText ());
    355.                 }
    356.              
    357.         }
    358. }
    359.  
    Now the better version
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Text;
    5.  
    6. /**
    7.  *  GALAP - v 1.0a
    8.  *  DEVELOPER NOTES : Han Solo 05/04/2015
    9.  *  ==========================================
    10.  *  Note: Display high-scores as teletype message with color rotation
    11.  *  TO-DO : nothing, all done.
    12.  *
    13.  **/
    14.  
    15. public class HighscoreDisplay : MonoBehaviour
    16. {
    17.         public TextMesh[] HighScoresLineMesh = null;
    18.         public float DisplayTime = 20.0f, letterPause = 0.01f;
    19.         private float _timer = 0.0f;
    20.         private string text, text2, message, message2 = null;
    21.         private GameObject galaxypilotstext;
    22.         private TextMesh pilotsLeader;
    23.         private string[] RankMarker = {"ST","ND","RD","TH"};
    24.         private int rankStep = 0, recordsToShow = 10;
    25.  
    26.         void Start ()
    27.         {
    28.                 _timer = 0.0f;
    29.                 if (Application.loadedLevelName == "MainMenu") {
    30.                         galaxypilotstext = GameObject.Find ("Pilots");
    31.                 }
    32.         }
    33.  
    34.         void OnEnable ()
    35.         {
    36.                 StartCoroutine (DelayScores ());
    37.         }
    38.    
    39.         IEnumerator ShowScores ()
    40.         {
    41.                 GameManager gameManager = GameManager.instance;
    42.                 if (gameManager != null) {
    43.                         List<HighScoreEntry> highScores = gameManager.GetHighScores ();
    44.                         if (highScores != null) {
    45.                                 for (int i = 0; i < recordsToShow; ++i) {
    46.                                         StringBuilder HighScoresLine = new StringBuilder (i);
    47.                                         HighScoreEntry hse = highScores [i];
    48.                                         if (hse != null && hse.Name != null) {
    49.                                                 HighScoresLine.Append (i + 1).Append (RankMarker [rankStep]).Append (" - ").Append (hse.Name).Append (" > ").Append (hse.Score);
    50.                                                 message = HighScoresLine.ToString ();
    51.                                         }
    52.                                         if (HighScoresLineMesh [i] != null) {
    53.                                                 foreach (char letter in message.ToCharArray()) {
    54.                                                         text += letter;
    55.                                                         HighScoresLineMesh [i].text = text;
    56.                                                         yield return 0;
    57.                                                         yield return new WaitForSeconds (letterPause);
    58.                                                 }
    59.                                                 if (rankStep < RankMarker.Length - 1) {
    60.                                                         rankStep++;
    61.                                                 }
    62.                                                 message = "";
    63.                                                 text = "";
    64.                                         }
    65.                                 }
    66.                         }
    67.                 }
    68.         }
    69.  
    70.         void Update ()
    71.         {
    72.                 if (Application.loadedLevelName == "MainMenu") {
    73.                         _timer += Time.deltaTime;
    74.                         if (SceneManager_MainMenu.instance != null) {
    75.                                 if (_timer > DisplayTime) {
    76.                                         _timer = 0.0f;
    77.                                         rankStep = 0;
    78.                                         for (int i = 0; i < recordsToShow; ++i) {
    79.                                                 text = "";
    80.                                                 HighScoresLineMesh [i].text = text;
    81.                                         }
    82.                                         if (galaxypilotstext) {
    83.                                                 galaxypilotstext.GetComponent<MeshRenderer> ().enabled = false;
    84.                                                 pilotsLeader.text = "";
    85.                                         }
    86.                                         SceneManager_MainMenu.instance.NextScreen ();
    87.                                 }
    88.                         }
    89.                 }
    90.         }
    91.  
    92.         IEnumerator DelayScores ()
    93.         {
    94.                 yield return new WaitForSeconds (1);
    95.                 StartCoroutine (ShowScores ());
    96.                 if (galaxypilotstext) {
    97.                         galaxypilotstext.GetComponent<MeshRenderer> ().enabled = true;
    98.                         pilotsLeader = galaxypilotstext.GetComponent<TextMesh> ();
    99.                         message2 = "--- GALAXY PILOTS ---";
    100.                         foreach (char letter in message2.ToCharArray()) {
    101.                                 text2 += letter;
    102.                                 pilotsLeader.text = text2;
    103.                                 yield return 0;
    104.                                 yield return new WaitForSeconds (letterPause);
    105.                         }
    106.                         text2 = "";
    107.                 }
    108.         }
    109. }
    I hope you do this also otherwise I'm mental, cheers
     
    Last edited: Apr 6, 2015
  2. MagicZelda

    MagicZelda

    Joined:
    May 1, 2013
    Posts:
    90
    after a while u will be able to do the tidying as you go :) ie do version 2 the first time
     
    San_Holo likes this.
  3. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Well the thing is, it is always really good to organize and also optimize your code, that is the way which the things should be done, but this thing is just like a sickness, a while ago actually I was really adicted to writing organized and also modular. Yes it is a good thing, but you should always know your limits and avoid writing things which you may not use in further development. For example, I was writing a script which checks if the player has "x" item, also it can check if the player has that item in "y" amount. Returns true if the player grants desired conditions. I thought too big, I wrote a script which uses dictionaries and a lot of parameters inside it, lists and other stuff, can even able to check which item is acquired by player in which level, when it is acquired, when it is used. It was a perfect script (ofcourse, nothing is perfect, you can always improve, but the thing is it was perfect relative to the scripts that I wrote before) I wrote the script, and started using it on my levels. Turned out that only 4-5 static variables were enough for what I wanted to achieve and all those checks, lists, variables and other stuff was unnecessary, never used them. So my point is, always be organized, tidy, optimized, but before you start writing your scripts, first, always, but always plan them, you can never be %100 sure about the scripts tasks, ofcourse you may want to add/remove something later on, but always try to be accurate about the task of the script, never go too far just because you want to write a really cool & optimized manager, sometimes, little and "noobish" things are enough for your purposes. Cheers :)
     
    San_Holo likes this.