My First Verse Script - Per-Player Variables Held in Health

The first thing I wanted to try to do when I hopped into Verse was make player-specific variables. When I found out those weren’t a thing, I wanted to find a way to make them a thing. I haven’t tested this for multiple players but this should work if you throw in a for loop for each player’s index. I think it could also be modified using MaxHealth instead of Health if players die in the game and you don’t want the variables to reset. There’s a few things I want to clean up to make it a bit more robust but wanted to share!

For this example script the player variable is held in the hundredths digit of health. It should be able to store multiple variables using the different decimals place.

using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Verse.org/Native }
using { /EpicGames.com/Temporary/Diagnostics }
using { /Verse.org/Simulation }

log_HealthVariables:=class(log_channel){}

HealthVariables := class<concrete>(creative_device):
    Logger:log = log{Channel:=log_HealthVariables}
    @editable
    IncreaseVariableButton:button_device = button_device{}
    @editable
    CheckVariableButton:button_device = button_device{}

    OnBegin<override>()<suspends>:void=
        IncreaseVariableButton.InteractedWithEvent.Subscribe(IncreaseVariable)
        CheckVariableButton.InteractedWithEvent.Subscribe(CheckVariable)    

    IncreaseVariable(Player:player):void= 
        allPlayers:[]player = GetPlayers()
        if (fortCharacter:fort_character = allPlayers[0].GetFortCharacter[]):
            fortCharacter.SetHealth(fortCharacter.GetHealth()-0.01)
            Logger.Print("Character has {fortCharacter.GetHealth()} health")

    CheckVariable(Player:player): void = 
        allPlayers:[]player = GetPlayers()
        if (fortCharacter:fort_character = allPlayers[0].GetFortCharacter[]):
            var health : float = fortCharacter.GetHealth()
            var highernum : int = 0
            if{set highernum = Round[health*100]}
            var lowernum : int = 0
            if{set lowernum = (Round[health])*100}

            Logger.Print("Variable: {(highernum-lowernum)*-1}")
2 Likes

That’s a clever trick!

It should be possible to do this without the need to be clever by using a map that associates each player with a different value, written [player]t where t is the type that is associated with each player.

Here’s an example for how to write your device class using such a map:

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

log_player_variable_device:=class(log_channel){}

player_variable_device := class<concrete>(creative_device):
    Logger:log = log{Channel:=log_player_variable_device}
    @editable
    IncreaseVariableButton:button_device = button_device{}
    @editable
    CheckVariableButton:button_device = button_device{}

    OnBegin<override>()<suspends>:void=
        IncreaseVariableButton.InteractedWithEvent.Subscribe(IncreaseVariable)
        CheckVariableButton.InteractedWithEvent.Subscribe(CheckVariable)    

    var PlayerToVariableMap:[player]int = map{}

    IncreaseVariable(Player:player):void=
        if(Value := set PlayerToVariableMap[Player] += 1):
            Logger.Print("Increased player's variable to {Value}.")
        else if(set PlayerToVariableMap[Player] = 0):
            Logger.Print("Initialed player's variable to 0.")

    CheckVariable(Player:player):void= 
        if (Value := PlayerToVariableMap[Player]):
            Logger.Print("Player variable's value is {Value}.")
        else:
            Logger.Print("Player variable's value is uninitialized.")
2 Likes

Wow, that’s much easier! Thanks for sharing that. Would the map variables be saved or do they only last per session?

The map variables won’t be saved across sessions.

1 Like