Search Unity

How do I protect myself from editing a variable outside a specific function?

Discussion in 'Scripting' started by Marscaleb, May 15, 2021.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    I have a variable in my script to designate certain states the player is in. Ideally I would only change that variable within a function I made for that purpose, which also does a couple other things to keep auxiliary effects in line.

    Is there a way I can keep this variable form being directly changed? Some kind of protection I can set up so I don't forget and accidentally just set it directly?
    I can do this with other scripts by not making a variable public, but I'm talking about internally within a single script. And I can't make the variable only exist within the function because then I can't read the value of the variable.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Usually you just preface it with underscore but some versions of intellisense will offer those variables up even if you don't type the underscore. Either way, at least the underscore draws your attention to it being unusual.
     
  3. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,074
    First of all make a variable private and yes in this case you can use underscore.
    Now never use this variable (you can name it _DONT_USE_ME_nameOfVariable) except of your method.
    Create Property and use it by this property.

    example with float variable named "nameOfVariable"
    Code (csharp):
    1.  
    2. private float _DONT_USE_ME_nameOfVariable
    3.  
    4. public float NameOfvariable{
    5. get{return _DONT_USE_ME_nameOfVariable;}
    6. }
    7.  
    8.  
     
    Kurt-Dekker likes this.
  4. You can always make a subclass. Just ask yourself the question, is it really worth the added complexity?
     
    Kurt-Dekker likes this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    Well, not a subclass in the formal sense. just another class. And often it's worth it just for grouping and for exactly what the OP is asking -- keeping the darn variables from polluting the dropdowns. Our player may have a section for attacking, with lots of vars, which could be broken out into another class:
    Code (CSharp):
    1. // inside of player:
    2. class attackHandler_t {
    3.   float nextAttackTime;
    4.   int currentComboLength;
    5.   int animationStage;
    6.   // more even more obscure variables
    7.  
    8.   public bool isReadyToAttack() { ... }
    9.   public beginNewAttack() { ... }
    10.   public updateAttack() { ... }
    11. }
    12.  
    13. attackHandler_t attacker;
    If can be nice having the real script call
    attacker.updateAttack();
    instead of merely calling
    updateAttack();
    and having all of those funny-looking variables be public.

    It saves you from needing names like
    _ATTACK_MODULE_nextTime;
    to make sure the wrong person doesn't use it by mistake. The drawback is variables which are 1/2-way. _Who_ we're attacking probably won't go in here, but we may need to know it for the animation. Do we get a copy, or a back-pointer back to the player? Ick.