Practice Time: Declaring Variables and Constants
Let’s say you’re making a game with magic potions. Potions have different effects — they can heal players or hurt them. You would want to declare and initialize some constants and variables to store player health, and the amount of damage or healing caused by a potion.
But which should be constants, and which should be variables?
| | |
| --- | --- |
| | var PlayerHealth: float = 100.0 |
| | PotionDamageAmount: float = 20.0 |
| | PotionHealAmount: float = 10.0 |
var PlayerHealth: float = 100.0 PotionDamageAmount: float = 20.0 PotionHealAmount: float = 10.0
Copy full snippet(3 lines long)
The variables and constants you're changing here won’t affect your Fortnite character's health, but you will learn how to do exactly that in a later lesson!
The player’s health will change during the game depending on what potions they use. When you think change, think variable.
On the other hand, it doesn’t make much sense to change the amount of damage or healing the potions do during the game, so those should be declared as constants.
-
If you write code to change the
PlayerHealthvariable now, you won’t be able to tell if it worked unless you print something to the log. To help with that, declare and initialize the string constant and string variable below. Remember to add the space between the end of the text and the last".| | | | --- | --- | | | PlayerStatusText: string = "Player health now " | | | var EffectOnPlayerText: string = "damaged " |PlayerStatusText: string = "Player health now " var EffectOnPlayerText: string = "damaged "
Copy full snippet(2 lines long)
-
Now you’re ready to change the player’s health. To change a variable’s value, you have to use the
setkeyword at the beginning of the expression. ThePotionDamageAmountshould be subtracted fromPlayerHealth, so use the-operator.set PlayerHealth = PlayerHealth - PotionDamageAmountset PlayerHealth = PlayerHealth - PotionDamageAmount
Copy full snippet(1 line long)
-
After
PlayerHealthchanges, you want to see the proof! To do that, make the calls toPrint()shown below.| | | | --- | --- | | | Print("The Player was {EffectOnPlayerText + ToString(PotionDamageAmount)}") | | | Print("{PlayerStatusText + ToString(PlayerHealth)}") |Print("The Player was {EffectOnPlayerText + ToString(PotionDamageAmount)}") Print("{PlayerStatusText + ToString(PlayerHealth)}")
Copy full snippet(2 lines long)
It may seem like you are trying to do math with your strings but you are actually combining with the
+operator. This is called concatenation. TheToString()function creates astringversion of yourfloatvariable and constant so they can be used byPrint(). -
Run this code now and you should see two new lines printed:
The Player was damaged 20.0Player health now 80.00
Now on your own, try to do the same thing for PotionHealAmount.
-
Change
PlayerHealthusing the correct keyword and operator. -
You’ll also need to change the
EffectOnPlayerTextvariable so that it makes sense when printed. -
Finally, you’ll need to print out how the player’s health was affected, and their current health. Try this by yourself first, but if you need help, look at the code below.
| | | | --- | --- | | | set PlayerHealth = PlayerHealth + PotionHealAmount | | | set EffectOnPlayerText = "healed " | | | Print("The Player was {EffectOnPlayerText + ToString(PotionHealAmount)}") | | | Print("{PlayerStatusText + ToString(PlayerHealth)}") |set PlayerHealth = PlayerHealth + PotionHealAmount set EffectOnPlayerText = "healed " Print("The Player was {EffectOnPlayerText + ToString(PotionHealAmount)}") Print("{PlayerStatusText + ToString(PlayerHealth)}")
Copy full snippet(4 lines long)
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 = |
| | |
| | PotionDamageAmount : float = 20.0 |
| | PotionHealAmount : float = 10.0 |
| | var PlayerHealth : float = 100.0 |
| | |
| | PlayerStatusText : string = "Player health now " |
| | var EffectOnPlayerText: string = "damaged " |
| | |
| | set PlayerHealth = PlayerHealth - PotionDamageAmount |
| | Print("The Player was {EffectOnPlayerText + ToString(PotionDamageAmount)}") |
| | Print("{PlayerStatusText + ToString(PlayerHealth)}") |
| | |
| | set PlayerHealth = PlayerHealth + PotionHealAmount |
| | set EffectOnPlayerText = "healed " |
| | Print("The Player was {EffectOnPlayerText + ToString(PotionHealAmount)}") |
| | Print("{PlayerStatusText + ToString(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 = PotionDamageAmount : float = 20.0 PotionHealAmount : float = 10.0 var PlayerHealth : float = 100.0 PlayerStatusText : string = "Player health now " var EffectOnPlayerText: string = "damaged " set PlayerHealth = PlayerHealth - PotionDamageAmount Print("The Player was {EffectOnPlayerText + ToString(PotionDamageAmount)}") Print("{PlayerStatusText + ToString(PlayerHealth)}") set PlayerHealth = PlayerHealth + PotionHealAmount set EffectOnPlayerText = "healed " Print("The Player was {EffectOnPlayerText + ToString(PotionHealAmount)}") Print("{PlayerStatusText + ToString(PlayerHealth)}")
Collapse code Copy full snippet(23 lines long)
Next Lesson
-
[

Lesson 4: Writing Simple Code
Practice is the key to competence. Practice writing more code!](/docs/documentation/en-us/fortnite/basics-of-writing-code-4-writing-simple-code-in-verse.md)