Return to your magic potion code from the last exercise. You will want to run the healing code when the player drinks a healing potion, and the damage code when they drink a damaging potion.
First You Write Some Code ...
Sounds like you'll have to answer a question about the type of potion, so you can create a new variable to keep track of that, a string called
PotionType.Versevar PotionType: string = "heal"Now you can ask if the variable
PotionTypeis equal to another string. When you ask ifPotionTypeis equal to"heal", the answer will be yes. The healing code would then run.Verseif (PotionType = "heal"): set PlayerHealth = PlayerHealth + PotionHealAmountNow write a similar if expression for the damage code.
Verseif (PotionType = "damage"): set PlayerHealth = PlayerHealth - PotionDamageAmountBut wait! There’s a problem with the healing code!
Nothing here stops a player from continuing to raise their health with potions. It’s a good idea to limit this, and now you can with
if!First, declare a new constant of type
float. Call itMaxHealthand set it to100.0.VerseMaxHealth: float = 100.0If the value of
PlayerHealthwould go over the value ofMaxHealthwhen a player drinks the healing potion, you'll need to setPlayerHealthtoMaxHealth.Verse# Code to run if player drinks a healing potion # If the player's health would not exceed MaxHealth if they were healed, # heal them the full amount if (PotionType = "heal" and (PlayerHealth + PotionHealAmount) < MaxHealth): set PlayerHealth = PlayerHealth + PotionHealAmount else: set PlayerHealth = MaxHealthNotice that the code above has an
andto ask if thePotionTypeis equal to"heal"and if thePlayerHealth + PotionAmountis less thanMaxHealth. Both of these conditions need to succeed for the PlayerHealth to be increased by the PotionHealAmount.It may not be clear to someone reading this code that it’s meant to prevent PlayerHealth from exceeding MaxHealth. That’s where code comments come in. Note the three lines of comments above the actual code.
The damage code works, but it can be improved with the use of
if…else if…else. If a player drinks a damage potion that would make PlayerHealth0.0or less, we want to set PlayerHealth to1.0instead. If PlayerHealth is already1.0, then we set it to0.0. This teaches the player the potion is harmful without being too punishing.Verse# Code to run if player drinks a damaging potion # Don't eliminate the player if their health is above MinHealth but below PotionDamageAmount # If they are already equal to or below MinHealth, eliminate them if (PotionType = "damage" and PlayerHealth > PotionDamageAmount): set PlayerHealth = PlayerHealth - PotionDamageAmount else if (PlayerHealth > MinHealth): # Give player one more chance if their health is low set PlayerHealth = 1.0 else: set PlayerHealth = 0.0
... Then You Find the Bug
Below you’ll find all of the code from this exercise, with some Print() function calls added for testing. Try running this code. Feel free to change the Print() calls to whatever you like. With the PotionType variable initialized to “heal”, you can expect that only the healing code will run.
But wait, there’s a bug!
Run the code below and see if you can find it.
MaxHealth: float = 100.0
MinHealth: float = 1.0
var PotionType: string = "heal"
# Code to run if player drinks a healing potion
# If the player's health would not exceed MaxHealth if they were healed,
# heal them the full amount
if (PotionType = "heal" and (PlayerHealth + PotionHealAmount) < MaxHealth):
set PlayerHealth = PlayerHealth + PotionHealAmount
Print ("Full heal")
If PotionType is set to heal, only the healing code should run. However, the if … else if … if expressions in the damage code are still being executed. This means that if PlayerHealth is greater than MinHealth, the PlayerHealth will get set to 1.0. This is not what you want, but you can fix it by nesting the other checks within if expressions that only check PotionType.
# Code to run if player drinks a healing potion
# If the player's health would not exceed MaxHealth if they were healed,
# heal them the full amount
if (PotionType = "heal"):
if ((PlayerHealth + PotionHealAmount) < MaxHealth):
set PlayerHealth = PlayerHealth + PotionHealAmount
Print ("Full heal")
else:
# set PlayerHealth the MaxHealth
set PlayerHealth = MaxHealth
Now, only the block of code indented under one of the if expressions that check PotionType will run. Try this code with PotionType set to "damage" as well.
Whew! That’s a lot of code changes, but you did it!
Take a break, drink some po- er… water, and come back when you’re ready for the next lesson.
Complete Script
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
hello_world_device := class(creative_device):
# Runs when the device is started in a running game
OnBegin<override>()<suspends> : void =
MaxHealth : float = 100.0
MinHealth : float = 1.0