0
\$\begingroup\$

I have been learning a new method of programming. I have been programming many years and am recently finding out there is a better method in general via abstract classes.

My question is how to convert a small section of my code so i can understand how this works entirely. So far i could only figure out how to do 1 part:

public abstract class Water
{
    public abstract string GetString(double AmountToAdd);
}

public class AddTotalWater : Water
{
    public double TotalWater;

    public override string GetString(double AmountToAdd)
    {
        TotalWater += AmountToAdd;
        if (TotalWater > 999999)
            return TotalWater.ToString(Globals.ScientificNotationFormatter);
        else
            return TotalWater.ToString();
    }
}

and use it with:

TotalWater.text = AddTotalWater.GetString(amountToAdd);

I would just like some guidance to learn how to do this fully. Is this the proper way to do this to begin with?

This is the nested nightmare I have currently that i want to convert:

public void DetermineWaterValues()
    {
        if (Time.deltaTime < TotalWaterProductionTime)
        {
            double tempWaterTime = waterCounter += Time.deltaTime;
            if (tempWaterTime > TotalWaterProductionTime)
            {
                tempWaterTime = TotalWaterProductionTime;
                waterCounter = 0;
                WaterProgressBar.fillAmount = 0;
                TotalWater.text = AddTotalWater.GetString(amountToAdd);
            }
            else
            {
                if (TotalWaterProductionTime >= 0.1)
                    WaterProgressBar.fillAmount = (float)(tempWaterTime / TotalWaterProductionTime);
                else
                    WaterProgressBar.fillAmount = 1;
            }
        }
        else
        {
            WaterProgressBar.fillAmount = 1;
            TotalWater.text = AddTotalWater.GetString(amountToAdd);
        }
    }

It's quite complicated to figure out how to do this properly. This is the way i did it for over 10 years heh...

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Honestly that’s not really a nightmare, at least not structurally. I recommend you bring this code snippet over to Code Review SE and they might have some suggestions for how best to get this code looking good in all respects. \$\endgroup\$ Commented May 19, 2019 at 13:52
  • 1
    \$\begingroup\$ It's unclear to me what you're trying to accomplish through the use of this abstract class. Can you describe the player-facing behaviour you want from this feature? It looks like there might be substantially simpler ways to achieve it. \$\endgroup\$ Commented May 19, 2019 at 15:53
  • \$\begingroup\$ Maybe i've just been reading things that don't matter. This is where it started: softwareengineering.stackexchange.com/questions/329971/… and then i found: linkedin.com/pulse/if-less-programming-c-jiri-pokorny apparently it's a big thing?! the linkedin guy quotes: we can eliminate 90% of bugs and 99% technical debt. i put it in gamedev because i'm mentioning a bit of unity code. \$\endgroup\$ Commented May 19, 2019 at 16:38

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.