Nate M
Nate M

Reputation: 49

Avoiding global variables for Unity. How and why?

I've seen multiple warnings about usage of global/public static variables for my code. I was wondering how important is it? How many global variables can I get away with using or should I attempt to use none at all?

And if so how do you solve this issue? For example: I have a "Stamina bar" in my game. There's a double variable corresponding to it, the code for the bar itself references the variable to change its size to represent the lower stamina.

        void Update()
        {
            if (FourEvents.CHARGE < 10)
            {
                transform.localScale = new Vector2((float)(FourEvents.CHARGE / 3.79), .76f);
            }
        }

Another gameObject refills the stamina bar through its collision:

        void OnCollisionEnter2D(Collision2D collision)
        {
            if (FourEvents.CHARGE <= 9)
            {
                FourEvents.CHARGE += 1;
                Debug.Log(FourEvents.CHARGE);
            }
        }

Although there's other codes referencing this variable, I'll avoid posting them to save space. Lastly:

        public static double CHARGE;
        public int counter;
        // Start is called before the first frame update
        void Start()
        {
            CHARGE += 10;
        }

        // Update is called once per frame
        void FixedUpdate()
        {
            counter += 1;
            if (counter > 1000)
            {
                counter = 0;
            }
            if (Input.GetKey(KeyCode.Space) && counter % 10 == 0)
            {
                if (CHARGE >= 1)
                {
                    CHARGE = CHARGE - .4;
                    Debug.Log(CHARGE);
                }
                else
                {
                    CHARGE = 0;
                }
            }
        }

Upvotes: 1

Views: 1643

Answers (1)

teslae
teslae

Reputation: 239

A solution that encapsulates the data of charge logic: Create a class which handles your charge logic

class ChargeHandler
{
    double charge;
    int chargeRefillLimit = 9;
    double initChargeValue = 10;
    public double GetCurrentCharge()
    {
        return charge;
    }

    public void RefillCharge()
    {
        if(charge <= chargeRefillLimit)
        {
            charge += 1;
        }
    }

    public void InitCharge()
    {
        charge = initChargeValue;
    }

    public void PerformCharge()
    {
        if(charge >= 1)
        {
            charge -= 4;
        }
        else
        {
            charge = 0;
        }
    }
}

and then use it like so:

ChargeHandler chargeHandler = new ChargeHandler();

void Start()
{
    chargeHandler.InitCharge();
}
void OnCollisionEnter2D(Collision2D collision)
{
    chargeHandler.RefillCharge();
}
void FixedUpdate()
{
    counter += 1;
        if (counter > 1000)
        {
            counter = 0;
        }
        if (Input.GetKey(KeyCode.Space) && counter % 10 == 0)
        {
             chargeHandler.PerformCharge();
        }
}

Upvotes: 1

Related Questions