Potential issues with my version of the Code?

Good morning!

I did the whole thing before Same offered the solution to the challenge and since nothing is breaking (yet), I wanted you learned opinion about my version:

  • I decided to reward Inside the Die() method
public void TakeDamage(GameObject instigator,float damage)
        {
            currentHealth= Mathf.Max(currentHealth-damage, 0);
            print(currentHealth);
            if(currentHealth<= 0)
            {
                if(instigator!=null)
                {
                    TriggerDeath(instigator);
                }
                else
                {
                    TriggerDeath(null);
                }
            }            
        }
private void TriggerDeath(GameObject theOnceWhoDidTheKilling)

        {

            if(isDead) return;

            GetComponent<Animator>().SetTrigger("dying");

            GetComponent<CapsuleCollider>().enabled= false;

            GetComponent<ActionScheduler>().CancelCurrentAction();

            RewardExperience(theOnceWhoDidTheKilling);

            isDead = true;

        }
  • If it was the enemy killing us or something else killing the enemy
void RewardExperience(GameObject killer)
        {
            float experienceToReward = baseStats.GetExperienceFromBaseStats();
            if(killer== null) return;
            PlayerExperience playerExperience= killer.GetComponent<PlayerExperience>();
            if(playerExperience == null) return;
            playerExperience.GainExperience(experienceToReward);
        }
  • To prevent death on load from awarding undeserved XP
public void RestoreState(object state)

        {

            currentHealth = (float) state;

            if (currentHealth <= 0)

            {

                TriggerDeath(null);

            }  

        }

So I am not too sure how It would go if for example I want to gain XP if something else kills the enemy after I just damage it. Will the code eventually break something?

I can’t think of anything that would cause this to break offhand (I can’t guarantee it, because by the end of this course, there will be a lot of code, but this still looks solid). Essentially, you’re just spinning off some of the responsibility to a different method.

A quick logic tip, though:

        if(currentHealth<= 0)
        {
            if(instigator!=null)
            {
                TriggerDeath(instigator);
            }
            else
            {
                TriggerDeath(null);
            }
        } 

is redundant. You can replace the entire if/else statement like this:

        if(currentHealth<= 0)
        {
            TriggerDeath(instigator);
        } 

The reason is that when you’re checking to see if instigator is null, you’re passing the reference if it isn’t, or null if it is… but if it’s already null (which is what we’re checking for), then TriggerDeath(instigator) == TriggerDeath(null);

1 Like

Oh I see I see! I totally overlooked that but now that you point it out…
Thanks Brian!

Now that you see it, you can’t unsee it. :slight_smile: Eventually, you’ll spot these little logic traps automatically.
Of course, if you were doing almost anything else based on the null reference, you’d still need the if/else code block. (If you had Debug.Logs in there, for example).

Thank you for your quick and precise answers! But I have another question if you feel generous: after the entire RPG course (inventory, quest included) I took Math for Videogames and also the Basics of C++ (all from Gamedev.TV). Which one should I take before the other to get the most out of them in your opinion?

I think it depends on your eventual goals. C++ is a handy language to know (and you have a little head start knowing C#), and if you’re looking long term to move to the Unreal Engine, this is a good starting point. Math for Videogames is also extremely important. Obviously, I’m going to recommend taking all fo the RPG series first (I’m biased), then it’s dealer’s choice whether it’s Math for Games or C++.

Hey thank you so much for the info. I will definitely finish the RPG series before moving on to something else!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms