Search Unity

What properties are useful for?

Discussion in 'Scripting' started by SirBoboHobo, May 29, 2015.

  1. SirBoboHobo

    SirBoboHobo

    Joined:
    Feb 26, 2015
    Posts:
    41
    Hey, I've decided to rewatch all scripting tutorials so i can understand it better.
    I encountered a video about properties but it got me a little confused, What are they exactly, why to use them (There are other ways to do it. i never seen anyone use properties) and are there any example where i would put them in my game...
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You've seen them. GameObject.transform is a property :)

    The main advantage is that you can control visibility of your getters and setters. Have you noticed that you can't do this?
    Code (csharp):
    1.  
    2. gameObject.transform = someOtherTransform;
    3.  
    You can also put logic in your getters and setters. Sometimes, a get won't even hold a value in memory but rather evaluate it and just return the result.
     
    sluice likes this.
  3. Deleted User

    Deleted User

    Guest

  4. SirBoboHobo

    SirBoboHobo

    Joined:
    Feb 26, 2015
    Posts:
    41
    In the tutorial in Unity, he shows as example Experience, now, in a game you would do the same? Like if I kill a monster I get 5 EXP, so I get the experience and in set return it with +5? Why not use just experience += 5;?
    I understand better with examples on where should I use them, It seems a little confusing using them for tasks that can be achieved without the use of them...
     
  5. Deleted User

    Deleted User

    Guest

    Well for experience you might want to only be able to ADD experience and not subtract it, and if you subtract it, you might want experience to go no lower than the minimum experience for that level.

    So you could do something like this for scenario 1:

    Code (CSharp):
    1. public int _experience;
    2. public int Experience { get {return _experience;} set {
    3.     if (value>=0) _experience += value;
    4.     if (...) // levelup!
    5. }
    6. }
     
  6. SirBoboHobo

    SirBoboHobo

    Joined:
    Feb 26, 2015
    Posts:
    41
    I'm not specifically talking about HOW to do it, I'm talking about WHY do it, I mean, why use properties in the first place? if I could just use a static int experience, then increment it whenever i want, then make a switch statement for the amount of experience thus determine the level of the player, I could basically do everything without properties, could anyone explain why to use them?
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Didn't I do that?

    The example you're giving is a bad use case (not your fault, you didn't write it after all). I'd do it this way

    Code (csharp):
    1.  
    2. public int Experience { get; private set; }
    3.  
    4. public void AddExperience(int amount)
    5. {
    6.     Experience += amount;
    7.     if (Experience > NextLevelAmount)
    8.     {
    9.         Experience = Experience - GetRequiredExperience(PlayerLevel);
    10.         PlayerLevel++;
    11.         NextLevelAmount = GetRequiredExperience(PlayerLevel);
    12.     }
    13. }
    14.  
    No other code can directly modify experience gained. You have to go through the AddExperience method which will calculate levelling and reset overflow experience as necessary. But I can still get the value to display in UI or whatever.
     
    SirBoboHobo and Deleted User like this.
  8. SirBoboHobo

    SirBoboHobo

    Joined:
    Feb 26, 2015
    Posts:
    41
    Thanks, that clears stuff out :)