Search Unity

can someone help me shorten my code?

Discussion in 'Scripting' started by SamDream, Jul 3, 2020.

  1. SamDream

    SamDream

    Joined:
    Jul 3, 2020
    Posts:
    1
    I'm annoyed because I am almost certain there is an easier and shorter way to write this code but I'm not able to figure it out. can anyone help me out?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class TimeCTRL : MonoBehaviour
    7. {
    8.     public int day;
    9.     public int month;
    10.     public int year;
    11.  
    12.     public Text daytxt;
    13.     public Text monthtxt;
    14.     public Text yeartxt;
    15.  
    16.     public Button endturn;
    17.  
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         day = 1;
    23.         month = 1;
    24.         year = 1;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         daytxt.text = "D" + day.ToString();
    31.         monthtxt.text = "M" + month.ToString();
    32.         yeartxt.text = "Y" + year.ToString();
    33.     }
    34.  
    35.     public void turn()
    36.     {
    37.         day += 1;
    38.         if (month == 1 && day == 31)
    39.         {
    40.             month += 1;
    41.             day = 1;
    42.         }
    43.  
    44.         else if (month == 2 && day == 28)
    45.         {
    46.             month += 1;
    47.             day = 1;
    48.         }
    49.  
    50.         else if (month == 3 && day == 31)
    51.         {
    52.             month += 1;
    53.             day = 1;
    54.         }
    55.  
    56.         else if (month == 4 && day == 30)
    57.         {
    58.             month += 1;
    59.             day = 1;
    60.         }
    61.  
    62.         else if (month == 5 && day == 31)
    63.         {
    64.             month += 1;
    65.             day = 1;
    66.         }
    67.  
    68.         else if (month == 6 && day == 30)
    69.         {
    70.             month += 1;
    71.             day = 1;
    72.         }
    73.  
    74.         else if (month == 7 && day == 31)
    75.         {
    76.             month += 1;
    77.             day = 1;
    78.         }
    79.  
    80.         else if (month == 8 && day == 31)
    81.         {
    82.             month += 1;
    83.             day = 1;
    84.         }
    85.  
    86.         else if (month == 9 && day == 30)
    87.         {
    88.             month += 1;
    89.             day = 1;
    90.         }
    91.  
    92.         else if (month == 10 && day == 31)
    93.         {
    94.             month += 1;
    95.             day = 1;
    96.         }
    97.  
    98.         else if (month == 11 && day == 30)
    99.         {
    100.             month += 1;
    101.             day = 1;
    102.         }
    103.  
    104.         else if (month == 12 && day == 31)
    105.         {
    106.             year += 1;
    107.             month = 1;
    108.             day = 1;
    109.         }
    110.     }
    111. }  
    112.  
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,190
    For calendars that are identical to the real world calendar you can just use System.DateTime.

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime

    Code (csharp):
    1. public void turn()
    2. {
    3.     System.DateTime currentDate = new System.DateTime(year, month, day);
    4.     System.DateTime futureDate = currentDate.AddDays(1);
    5.  
    6.     day = futureDate.day;
    7.     month = futureDate.month;
    8.     year = futureDate.year;
    9. }
    For calendars that are not identical to the real world calendar the way you wrote the code is the simplest approach with anything more complex needing justification to be written (eg will you be using multiple different calendars). I wouldn't worry about shortening it for non-standard calendars. You won't see a performance difference and it's already readable.
     
    Last edited: Jul 3, 2020
  3. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    If you want to keep the rough idea of the code as an exercise or something (instead of replacing it with System.DateTime), you can consider making an array indexed by month which holds the length of those months

    int monthLength = MonthLengths[month];
    if (day >= monthLength) {
    month += 1;
    day = 1;
    }
    if (month == 13) {
    month = 1;
    year += 1;
    }

    (note that this array would be one-indexed, so like [ 0, 31, 28, 31 , ... ] )