Your HurtPlayer() function is working, but since you’re passing a literal value to the Damage() method, that value can only change when you manually change the code. In this exercise, you’re going to make your HurtPlayer() function more flexible with the return value from another function that you will create.
In Lesson 4: Practice Time, you used conditionals to calculate how much damage or healing should be done by potions, depending on the player’s health. You're going to bring some of that code back in a function called CalculateDamage(). It will return a value of type float. You will store that value in a constant that you will then pass to the Damage() method.
Create a function named
CalculateDamage(). Within the function, declare two constants of typefloat:MinHealth, andPotionDamageAmount. Declare one variable of typefloatcalledPlayerHealth.VerseCalculateDamage() : float = MinHealth : float = 1.0 PotionDamageAmount: float = 80.0 var PlayerHealth : float = 100.0Create an
if…else if…elseexpression that returns one of three values:PotionDamageAmount, ifPlayerHealthis greater thanPotionDamageAmountPlayerHealth-MinHealth, ifPlayerHealthis less thanPotionDamageAmountbut greater thanMinHealthPlayerHealth, ifPlayerHealthis less than or equal toMinHealthVerse# If the damage amount would not eliminate the player, do that amount of damage if (PlayerHealth > PotionDamageAmount): return PotionDamageAmount else if (PlayerHealth > MinHealth): # Give player one more chance if their health is low return PlayerHealth - MinHealth else: # Eliminate player return PlayerHealth
The effect of this code is that it will give the player a second chance if they drink a potion that would normally eliminate them.
Now that you have made a function that returns a useful result, you need to store that result. Declare a constant
floatcalledDamageToDo, and initialize it with the return value ofCalculateDamage(). Add this to yourHurtPlayer()function.VerseHurtPlayer() : void = Playspace : fort_playspace = GetPlayspace() AllPlayers : []player = Playspace.GetPlayers() if (FirstPlayer : player = AllPlayers[0]): if (FortniteCharacter : fort_character = FirstPlayer.GetFortCharacter[]): DamageToDo: float = CalculateDamage() FortniteCharacter.Damage(50.0)Finally, time to use the result! Pass
DamageToDoas an argument toDamage(). Add aPrint()so you can see exactly whatCalculateDamage()is returning.VerseHurtPlayer() : void = Playspace : fort_playspace = GetPlayspace() AllPlayers : []player = Playspace.GetPlayers() if (FirstPlayer : player = AllPlayers[0]): if (FortniteCharacter : fort_character = FirstPlayer.GetFortCharacter[]): DamageToDo: float = CalculateDamage() Print("DamageToDo: {DamageToDo}") FortniteCharacter.Damage(DamageToDo)
Now instead of your call to the Damage() method using a literal value, it uses the value returned from CalculateDamage(). You should have already written the call to HurtPlayer() from a previous exercise, but don’t forget to check.
Complete Script
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Playspaces }
using { /Verse.org/Simulation }
using { /Verse.org/Verse }
hello_world_device := class(creative_device):
# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void=
On your own, try to create a function similar to CalculateDamage() that returns a result that would be useful for HealPlayer(). If you get stuck, check the Lesson 4 Practice Time for some ideas.