Question Absolute Beginner

DanTheManForth

New member
Joined
Sep 6, 2018
Messages
2
Programming Experience
Beginner
Hello all. I literally just started learning C# from knowing less than zero about coding at all. I am trying to program a basic FPS health system and am struggling to figure out things like coding inversely proportional relationships and making processes stop after reaching a certain level: the "health" and "acumen" aren't supposed to increase when "thirst" or "hunger are at max... but they do anyway. And "health" and "acumen" are supposed to stop regenerating when they hit max ... but they don't. I also cant figure out how to make "acumen" go down at the same rate that "pain" goes up. And when "health" gets to zero the player is supposed to "die" and everything stop ... but "health" keeps going down past zero and it keeps dying infinitely instead.

I have pasted my code below, in the hopes that someone could tell me what I am doing wrong or even just doing poorly. I kinow I am probably missing something obvious, and I apologize for the glaring amateurish language.




C#:
{


    // variables
    //private float maxHealth, maxThirst, maxHunger, maxPain, maxAcumen;
    public float maxHealth, maxThirst, maxHunger, maxPain, maxAcumen;
    public float thirstIncreaseRate, hungerIncreaseRate, healthIncreaseRate, painIncreaseRate, acumenIncreaseRate;
    public float painDecreaseRate, healthDecreaseRate, acumenDecreaseRate;
    public float health, thirst, hunger, pain, acumen;
    private float maxAcumenIncreaseRate;


    private bool playerDeath;


    // functions
    public void Start()
    {
        health = maxHealth;
        acumen = maxAcumen;
        maxAcumenIncreaseRate = 0.0001f; // player max acumen increases appriximately 1 point per 3 hours






    }
    public void Update()
    {
        // thirst, hunger, health, acumen, and max acumen gradually increase
        if (!playerDeath)
        {
            thirst += thirstIncreaseRate * Time.deltaTime;
            hunger += hungerIncreaseRate * Time.deltaTime;
            maxAcumen += maxAcumenIncreaseRate * Time.deltaTime;
        }


        // health regenerates to max if hunger and thirst are below max
        if (thirst < maxThirst && hunger < maxHunger)
        {
            health += healthIncreaseRate * Time.deltaTime;
        }


        // acumen regenerates to max if hunger and thirst are below max and pain is zero
        if (thirst < maxThirst && hunger < maxHunger && pain <= 0f)
        {
            acumen += acumenIncreaseRate * Time.deltaTime;
        }
        
        // health and acumen gradually decrease when thirst and/or hunger are at max
        if (thirst >= maxThirst)
        {
            health += healthDecreaseRate * Time.deltaTime;
            acumen += acumenDecreaseRate * Time.deltaTime;
        }


        if (hunger >= maxHunger)
        {
            health += healthDecreaseRate * Time.deltaTime;
            acumen += acumenDecreaseRate * Time.deltaTime;
        }


        // pain gradually decreases over time
        if (pain >= maxPain)
        {
            pain += painDecreaseRate * Time.deltaTime;
        }


        // player dies at zero health
        if (health <= 0)
        {
            playerDeath = true;
            print("You Have Died");
        }


    }


 
}
 
Debugging is a skill that all developers need to learn very early on. Debugging doesn't entail simply reading your code to see if you can spot something and running to see what the result is. It involves running the code, setting breakpoints to halt execution at certain points and then stepping through the code line by line, examining the state at each step. You can then compare the actual path of execution and the state at each step to what you expect. The moment the actuality differs from the expectation, you have found an issue and you can examine that specifically. Even if you still can't fix the issue, you can at least tell us exactly where in your code the issue occurs and also - and this is the part that just reading the code makes difficult to impossible - exactly what the value of every relevant variable and expression was at the time. This is just one of many places you can learn how to debug:

https://csharp.net-tutorials.com/debugging/introduction/
 
Sounds like a fairly typical developer to me. We all make loads of mistakes like that but, fortunately, they are usually relatively easy to identify with basic debugging. Things like that are often hard to notice when simply reading code because you don't easily connect where the value is set and where it is used. If you can see the value being used while debugging though, something like a positive value where a negative is expected becomes fairly obvious. No developer can get very far without having decent debugging skills.
 
Back
Top Bottom