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".
You're reading a preview
The full reference is free for BrainDeadGuild Discord members — sign in to read it all, or open the original at the source.
Sign in with your BrainDead.TV / BrainDeadGuild Discord account for full access.