Still got your Verse code open? Good!
For this exercise, you’re going to call functions that will actually damage your Fortnite character, as promised all the way back in Lesson 3: Practice Time!
Before you start coding, you will need to add a device to your UEFN island. It's called the Player Spawner and it causes your player character to spawn at the location of the device instead of in the air, which is the default spawn location for Fortnite. In order for the code in this exercise to work, the Player Spawner is required.
To learn how to place the Player Spawner device, see Object Placement in UEFN Controls for Creative Users. Feel free to place it anywhere on your island.
Some of the code you'll use in this exercise will be new to you, but you'll have a chance to review what each line is doing. Don’t worry if you don’t understand it all right now. This practice is just about calling functions.
Make sure you have all of the following lines at the very top of your Verse file. These tell the Verse compiler which parts of built-in Verse and Fortnite code you’ll be using in your Verse-authored device.
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Playspaces }
using { /Verse.org/Simulation }
using { /Verse.org/Verse }
Next, you'll write the lines of code that damage your Fortnite character. Let’s go through this line by line.
Call a function named
GetPlayspace(). It returns a value of typefort_playspace, which you save to a constant namedPlayspace. You need this to get the players.Call a method named
GetPlayers()on the constantPlayspace. The method returns an array of typeplayer. An array is a container that can hold multiple variables of the same type in Verse. In this case, theAllPlayersarray is holding all the players in your level.You already learned that you can use
ifto ask yes or no questions in your code. In this example, you useifto ask if there is a variable at the first position of theAllPlayersarray. The expressionAllPlayers[0]will return the value of the variable at that position if it exists, and the constantFirstPlayerwill get initialized to the return value.A second
ifnested within the first asks if theFirstPlayerconstant has a Fortnite character by calling its methodGetFortCharacter[]. Notice the square brackets used to make the call. These are used to call a function that can fail. That’s whyifis used to call this function. IfGetFortCharacter[]succeeds in returning a value of typefort_character, the constant FortniteCharacter is initialized to that value.Finally, now that you have a constant of type
fort_characteryou can call its methodDamage(). This method takes one parameter of the typefloat. That’s the amount of damage the character will receive. You’ll learn more about parameters in the next lesson.VersePlayspace: fort_playspace = GetPlayspace() AllPlayers: []player = Playspace.GetPlayers() if (FirstPlayer : player = AllPlayers[0]): if (FortniteCharacter : fort_character = FirstPlayer.GetFortCharacter[]): FortniteCharacter.Damage(50.0)
If you run this code, you should see your Fortnite character take 50.0 damage when your game starts!
Pretty cool, right?
Complete Script
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Playspaces }
using { /Verse.org/Simulation }
using { /Verse.org/Verse }
hello_world_device := class(creative_device):
# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void=