Basics of Writing Code 4 Practice Time in Verse
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.var PotionType: string = "heal"var PotionType: string = "heal"
Copy full snippet(1 line long)
-
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.| | | | --- | --- | | | if (PotionType = "heal"): | | | set PlayerHealth = PlayerHealth + PotionHealAmount |if (PotionType = "heal"): set PlayerHealth = PlayerHealth + PotionHealAmount
Copy full snippet(2 lines long)
-
Now write a similar if expression for the damage code.
| | | | --- | --- | | | if (PotionType = "damage"): | | | set PlayerHealth = PlayerHealth - PotionDamageAmount |if (PotionType = "damage"): set PlayerHealth = PlayerHealth - PotionDamageAmount
Copy full snippet(2 lines long)
But 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.MaxHealth: float = 100.0MaxHealth: float = 100.0
Copy full snippet(1 line long)
If the value of
PlayerHealthwould go over the value ofMaxHealthwhen a player drinks the healing potion, you'll need to setPlayerHealthtoMaxHealth.| | | | --- | --- | | | # 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 = MaxHealth |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 = MaxHealth
Copy full snippet(7 lines long)
Notice 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.| | | | --- | --- | | | # 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 |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
Copy full snippet(10 lines long)
... 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") |
| | else: |
| | # else, set PlayerHealth the MaxHealth |
| | set PlayerHealth = MaxHealth |
| | Print("PlayerHealth too high for full heal") |
| | |
| | # 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 |
| | Print("Full damage") |
| | else if (PlayerHealth > MinHealth): |
| | # Give player one more chance if their health is low |
| | set PlayerHealth = 1.0 |
| | Print("PlayerHealth set to 1.0") |
| | else: |
| | # Eliminate player |
| | set PlayerHealth = 0.0 |
| | Print("Player eliminated!") |
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") else:
else, set PlayerHealth the MaxHealth
set PlayerHealth = MaxHealth Print("PlayerHealth too high for full heal")
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 Print("Full damage") else if (PlayerHealth > MinHealth):
Give player one more chance if their health is low
set PlayerHealth = 1.0 Print("PlayerHealth set to 1.0") else:
Eliminate player
set PlayerHealth = 0.0 Print("Player eliminated!")
Collapse code Copy full snippet(29 lines long)
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 |
| | Print("PlayerHealth too high for full heal") |
| | |
| | # 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"): |
| | if ((PlayerHealth > PotionDamageAmount)): |
| | set PlayerHealth = PlayerHealth - PotionDamageAmount |
| | Print("Full damage") |
| | else if (PlayerHealth > MinHealth): |
| | # Give player one more chance if their health is low |
| | set PlayerHealth = 1.0 |
| | Print("PlayerHealth set to 1.0") |
| | else: |
| | # Eliminate player |
| | set PlayerHealth = 0.0 |
| | Print("Player eliminated!") |
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 Print("PlayerHealth too high for full heal")
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"): if ((PlayerHealth > PotionDamageAmount)): set PlayerHealth = PlayerHealth - PotionDamageAmount Print("Full damage") else if (PlayerHealth > MinHealth):
Give player one more chance if their health is low
set PlayerHealth = 1.0 Print("PlayerHealth set to 1.0") else:
Eliminate player
set PlayerHealth = 0.0 Print("Player eliminated!")
Collapse code Copy full snippet(27 lines long)
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 |
| | var PotionType: string = "heal" |
| | |
| | set PlayerHealth = 80.0 |
| | |
| | # 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: |
| | # else, set PlayerHealth the MaxHealth |
| | set PlayerHealth = MaxHealth |
| | Print("PlayerHealth too high for full heal") |
| | |
| | # 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"): |
| | if ((PlayerHealth > PotionDamageAmount)): |
| | set PlayerHealth = PlayerHealth - PotionDamageAmount |
| | Print("Full damage") |
| | else if (PlayerHealth > MinHealth): |
| | # Give player one more chance if their health is low |
| | set PlayerHealth = 1.0 |
| | Print("PlayerHealth set to 1.0") |
| | else: |
| | set PlayerHealth = 0.0 |
| | Print("Player eliminated!") |
| | |
| | Print("PlayerHealth now {PlayerHealth}") |
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 var PotionType: string = "heal" set PlayerHealth = 80.0
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:
else, set PlayerHealth the MaxHealth
set PlayerHealth = MaxHealth Print("PlayerHealth too high for full heal")
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"): if ((PlayerHealth > PotionDamageAmount)): set PlayerHealth = PlayerHealth - PotionDamageAmount Print("Full damage") else if (PlayerHealth > MinHealth):
Give player one more chance if their health is low
set PlayerHealth = 1.0 Print("PlayerHealth set to 1.0") else: set PlayerHealth = 0.0 Print("Player eliminated!") Print("PlayerHealth now {PlayerHealth}")
Collapse code Copy full snippet(42 lines long)
Next Lesson
-
[

Lesson 5: Calling Functions
Why do the same work twice if you don't have to?](/docs/documentation/en-us/fortnite/basics-of-writing-code-5-calling-functions-in-verse.md)