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
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".VersePlayerStatusText: string = "Player health now " var EffectOnPlayerText: string = "damaged "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.Verseset PlayerHealth = PlayerHealth - PotionDamageAmountAfter
PlayerHealthchanges, you want to see the proof! To do that, make the calls toPrint()shown below.VersePrint("The Player was {EffectOnPlayerText + ToString(PotionDamageAmount)}") Print("{PlayerStatusText + ToString(PlayerHealth)}")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.
Verseset PlayerHealth = PlayerHealth + PotionHealAmount set EffectOnPlayerText = "healed " Print("The Player was {EffectOnPlayerText + ToString(PotionHealAmount)}") Print("{PlayerStatusText + ToString(PlayerHealth)}")
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