VerseIsland
FeedLearnLeaderboardGuidesReferenceExploreSearchDocsVerse Library
Sign in
Feed Learn 📖 Codex Leaderboard Guides Reference Explore Search Docs Verse Library
Browse Fortnite›Verse Language·Up to Fortnite · Verse Language·30 of 570 in Verse Language

Constants And Variables In Verse

Constants

A constant is a location where a value is stored, and its value cannot change during the runtime of the program.

To create a constant, you have to specify its identifier and type, known as declaring a constant, and provide a value for the constant, known as initializing a constant.

A constant must be initialized when it is declared, and must be declared before it can be used.

Constant creation has the following syntax:

Identifier : type = expression

Identifier : type = expression

Copy full snippet(1 line long)

Declaring and initializing a constant with explicit type in Verse

Constants declared in a function can omit the type:

Identifier := expression

Identifier := expression

Copy full snippet(1 line long)

Declaring and initializing a constant with inferred type in Verse

If the type is omitted, the constant's type is inferred from the expression used to initialize the constant. Only local constants can omit the type because the type describes how the constant can be used. A constant in a module makes up part of the interface of the module that contains it. Without the type, that interface is nonobvious.

In the following example, a random number is generated in each iteration of the loop and used to initialize the constant RandomNumber. The random number will only break out of the loop it is less than twenty.

|  |  |
| --- | --- |
|  | loop: |
|  | Limit := 20 |
|  | # For local constants, the type can be omitted. |
|  | RandomNumber : int = GetRandomNumber() |
|  | # Providing the type explicitly can make the code easier to read. |
|  | if (RandomNumber < Limit): |
|  | break |

loop: Limit := 20

For local constants, the type can be omitted.

RandomNumber : int = GetRandomNumber()

Providing the type explicitly can make the code easier to read.

if (RandomNumber < Limit): break

Copy full snippet(7 lines long)

Note that in each loop iteration, a new constant named RandomNumber is introduced and assigned the result of GetRandomNumber() as its value.

Variables

In addition to the constants described above, Verse also has variables.

Variables are similar to constants, but are defined with the keyword var, which means you can change their values at any point.

For example,

var MaxHealthUpgrade : int = 10

var MaxHealthUpgrade : int = 10

Copy full snippet(1 line long)

is an integer variable, and its value may not always be 10.

Variable creation has the following syntax:

var Identifier : type = expression

var Identifier : type = expression

Copy full snippet(1 line long)

Declaring and initializing a variable in Verse

Note that the type must be explicitly specified for variables.

After you create a variable, you can assign a different value to it with the following syntax:

set Identifier = expression

set Identifier = expression

Copy full snippet(1 line long)

Changing a variables value

Aside from =, a variety of other operators can be used to mutate a variable. For example,

|  |  |
| --- | --- |
|  | var X:int = 0 |
|  | set X += 1 |
|  | set X *= 2 |

var X:int = 0 set X += 1 set X *= 2

Copy full snippet(3 lines long)

Global Variables

A variable defined in a module is global to any game instance running where the variable is in scope.

One way to declare a module-scoped variable in Verse is to use the weak_map(session, t) type where the key type is the type of the current Fortnite island instance, or session, and the value type is any type t. For details on weak_map, see Map.

The following example shows how to create a global integer variable named GlobalInt that is incremented every time ExampleFunction() is called.

|  |  |
| --- | --- |
|  | using { /Verse.org/Simulation } # For session |
|  |  |
|  | var GlobalInt:weak_map(session, int) = map{} |
|  |  |
|  | ExampleFunction():void= |
|  | X := if (Y := GlobalInt[GetSession()]) then Y + 1 else 0 |
|  | if: |
|  | set GlobalInt[GetSession()] = X |
|  | Print("{X}") |

using { /Verse.org/Simulation } # For session var GlobalInt:weak_map(session, int) = map{} ExampleFunction():void= X := if (Y := GlobalInt[GetSession()]) then Y + 1 else 0 if: set GlobalInt[GetSession()] = X Print("{X}")

Copy full snippet(9 lines long)

Module-scoped variables using the session type as the key have the following limitations:

  • You can only access values for the current session you are in, not any other session.
  • The module-scoped variable weak_map cannot be completely read or written to, so it's not possible to read or override values for all sessions.
  • You cannot iterate through the values of a weak_map or see how many sessions are currently active, because a weak_map has no length.

Persistable Data

Except for module-scoped variables associated with the session, a module-scoped variable requires persistence, the storing of data beyond the current game.

You can declare a persistable variable in Verse using the type weak_map(player, t). Any type represented by t that is persistable can be stored and accessed for a particular player and the data can be visible to subsequent game sessions. Whenever a player joins a game, their previous saved data is loaded into all module-scoped variables of type weak_map(player, t).

In the following example, the global 'weak_map' variable MySavedPlayerData uses the player type as the key and an integer as the value. Once you’ve defined your persistable data, you’ll need to initialize the data for each player. You can do this by checking to see if there’s not already data stored for that player and then adding the player and an initial value to the weak_map.

using { /Fortnite.com/Devices }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/Simulation }

var MySavedPlayerData:weak_map(player, int) = map{}

# A Verse-authored creative device that can be placed in a level
persistence_example := class(creative_device):

    # Runs when the device is started in a running game

Expand code Copy full snippet(17 lines long)

Module-scoped variables using the player type as the key have the following limitations:

  • Accessing the player’s persistent data is only allowed when the player is in the current game.

  • If a player leaves a game or is not in the current session, you can no longer store or access their data in that game session. If the player returns or plays the same game again, then you can access and update their data.

  • The module-scoped weak_map cannot be completely read or written to, so it's not possible to read or override values for all players.

  • You cannot iterate through the values of a weak_map or see how many players have a record in the weak_map, because a weak_map has no length.

  • A single island can have up to two persistent variables, that is two weak_map variables with player as the key type.

  • At least one persistent variable's weak_map value must be a class if the limit for max persistent variables has been met.

    • A single weak_map record has a maximum data size of 128 kilobytes (KB) per player.

For more details on persistence in Verse, check out Using Persistable Data in Verse.

Guided course

Add this to your free study plan — we’ll suggest related material and stitch it into one compile-checked lesson.

Keep exploring
More in Verse Language · 30 of 570
Prev
concurrency overview in verse
Next
constructor in verse
Browse all files in this folder (A–Z)

Source

Epic Games documentation

View original

Last updated 2026-03-08T05:07:49.423826+00:00

VerseIsland· an archipelago of Verse & UEFN knowledge
🗺️ Island atlasLearnGuidesHistoryAbout & PressSources & LicensingPrivacy PolicyTermsStatus

Not affiliated with Epic Games. Fortnite, UEFN, Unreal Engine, and Verse are trademarks of Epic Games, Inc. Content is attributed to its source — see Sources & Licensing.

🦜

Scout · your island guide

The Isle of Verse