Giving each player a unique ID for the session

Hello guys,
I’m trying to figure out a way of giving each player a unique ID when they join the game. It only needs to last until the game finishes or they leave.
I’ll explain what i’m trying to achieve:
at the start of each round(10 rounds in total) I need to randomly select one player on each team that will be put on a different random loot pool.
so everyone else will have random loot from the same loot pool but the randomly selected player will have a random loot pool that will always grant them port-a-bunkers. I need at least one player on each team to receive bunkers. I need a way of detecting if a player leaves so that player doesn’t receive the bunker loot on the next round.
if this explanation isn’t clear I’ll try and explain it better, Thanks in advance!

Hi.
I don’t know if this helps but…

The unique ID could simply be a reference to that player as an agent type. When the game starts, you would create a map with the key as the agent reference and the value to whatever variable you want, an integer, a string, whatever. You could also use an array instead of a map and then randomly reference the array to get a reference to that player. The key idea is that every player is an agent and has a unique reference value. The agent type for each player is then the player’s unique ID. That ID can then be paired with whatever else you want in a map or an array. For what you want to do, it actually seems like an array would work better because you could randomly access any array index value, and that would reference a random player. Once that player was id’d, then you would do whatever you want to them.

These tutorials might help:

You could also do something simple as this:

Players:=GetPlayspace().GetPlayers()
Index:= GetRandomInt(0, Players.Length-1)
if (RandomPlayer:=Players[Index]):
RandomPlayer.SendToLobby() # or whatever you want to do

Awesome thank you! I didn’t get a notification for your reply, otherwise I would’ve responded sooner.
I’m assuming I then update the array when a player leaves the match?
Also how would I create arrays that are team specific?
Thank you

Players are unique by default, if you store a player type you can compare it later

var BunkerPlayerMaybe : ?player = false
GiveBunker(Player: player):void=
    # TODO: Grant item
    set BunkerPlayerMaybe = option{Player}

OnNewRound():void=
    EligiblePlayers := for(Player : GetPlayspace().GetPlayers(), not BunkerPlayerMaybe? or BunkerPlayerMaybe? <> Player) { Player }
    if(NewBunkerPlayer := Shuffle(EligiblePlayers)[0]):
        GiveBunker(NewBunkerPlayer)

More or less, it’s a dumb example coz it only stores one player but you get the gist

1 Like

Awesome thank you, how would I create the array to be team specific?

You can use a [team]player map or [int]player map, but also maybe you can just store an array of players, doesn’t seem to hurt performance much here

Sorry, I’m new to verse. So I could create a map with the individual Players as the keys and their team as the value? and then randomly select two players with different values and make them the NewBunkerPlayer?
Sorry if these are stupid questions, this is a little beyond my limited capabilities :joy:

Well you’re also new to coding then I guess, map keys are on the left side, which could lead to this usage

if(RandomPlayer <> BunkerPlayers[RandomPlayerTeam]):
    Print("RandomPlayer is not the previous bunker player or smth")

Not sure it’s the ideal solution, the array seem better imo, so that players can swap teams too

I am :confused: I’ve dabbled for a few years but far from a pro. I probably should’ve specified that, sorry.
is there a way I can get an array for the players on each team?
Example:

TeamCollection := GetPlayspace().GetTeamCollection()
TeamOnePlayers := TeamCollection.GetAgents(Team1)

1 Like

Yes, that’s the way, you’d just need to retrieve Team1 first by doing GetTeamCollection().GetTeams()[0] and then cast your agents as players

1 Like

Okay cool, I’ve done that, however I get an Error for GetAgents.
I’ve Also tried GetPlayers but I’m assuming that won’t work because it’s not part of fort_team_collection .
Also do I need to cast agents as players if I don’t intend on putting NPCs on the map?
thank you for your help, I’m sorry if these are stupid questions :slight_smile:

        TeamCollection := GetPlayspace().GetTeamCollection()
        TeamOne:= if (TeamCollection.GetTeams()[0]):
        TeamTwo := if (TeamCollection.GetTeams()[1]):
        TeamOnePlayers := TeamOne.GetAgents()
        TeamTwoPlayers := TeamTwo.GetAgents()

I’ve also tried this:
uefn code fail 2

I think I’ve solved it. I haven’t run into any bugs while testing. Thank you for your help! I wouldn’t have got there without your help.

Solution:

    DecideBunkerPlayer(Agent : agent):void=
        Print("Deciding Bunker Player")
        TeamCollection := GetPlayspace().GetTeamCollection()
        if (TeamOne := GetPlayspace().GetTeamCollection().GetTeams()[0]):
            if (TeamOnePlayers := GetPlayspace().GetTeamCollection().GetAgents[TeamOne]):
                Index := GetRandomInt(0, TeamOnePlayers.Length -1)
                if (RandomPlayer1 := TeamOnePlayers[Index]):
                    GrantBunker(RandomPlayer1)
                    Print("Bunker Granted")
        if (TeamTwo := GetPlayspace().GetTeamCollection().GetTeams()[1]):
            if (TeamTwoPlayers := GetPlayspace().GetTeamCollection().GetAgents[TeamTwo]):
                Index2 := GetRandomInt(0, TeamTwoPlayers.Length -1)
                if (RandomPlayer2 := TeamTwoPlayers[Index2]):
                    GrantBunker(RandomPlayer2)
                    Print("Bunker Granted")

    GrantBunker(Agent : agent):void=
        BunkGranter := FindCreativeObjectsWithTag(BunkerGrant{})
        for (Obj : BunkGranter):
            if (BunkGranterObj := item_granter_device[Obj]):
                BunkGranterObj.GrantItem(Agent)

1 Like

Wait you’re not storing the previously bunker picked player here ?

1 Like

Sorry, I probably didn’t explain what I wanted very well originally. The BunkerPlayer Only needs to be determined at the start of each round(first to win 10 Rounds). At the time I thought I needed to store a unique ID for each player to achieve that but you made me realize that as long as I can update the team 1 and 2 players arrays I don’t need to store anything. I have no idea if what I’ve done is the best or most efficient way of achieving this but It works :joy:
You’ve been a huge help, thank you! :slight_smile:

I’ve changed the code slightly, I shuffle the Team player array now and choose index 0:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /Fortnite.com/Game }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation/Tags }
using { /UnrealEngine.com/Temporary/Diagnostics }


RoundStartPC := class(tag){}

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

    @editable
    ItemGranterBunkerArray : []item_granter_device = array{}

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        InitPlayerCounter()

    DecideBunkPlayer():void=
        Print("Deciding Bunker Players")
        TeamCollection := GetPlayspace().GetTeamCollection()
        if (TeamOne := GetPlayspace().GetTeamCollection().GetTeams()[0]):
            if (TeamOnePlayers := GetPlayspace().GetTeamCollection().GetAgents[TeamOne]):
                if (BunkerPlayer1 := Shuffle(TeamOnePlayers)[0]):
                    BunkArrayIndex1 :=  GetRandomInt(0, ItemGranterBunkerArray.Length -1)
                    if (RandomBunkGranter1 := ItemGranterBunkerArray[BunkArrayIndex1]):
                        RandomBunkGranter1.GrantItem(BunkerPlayer1)
                        Print("Bunker Granted T1")
                        
         if (TeamTwo := GetPlayspace().GetTeamCollection().GetTeams()[1]):
            if (TeamTwoPlayers := GetPlayspace().GetTeamCollection().GetAgents[TeamTwo]):
                if (BunkerPlayer2 := Shuffle(TeamTwoPlayers)[0]):
                    BunkArrayIndex2 :=  GetRandomInt(0, ItemGranterBunkerArray.Length -1)
                    if (RandomBunkGranter2 := ItemGranterBunkerArray[BunkArrayIndex2]):
                        RandomBunkGranter2.GrantItem(BunkerPlayer2)
                        Print("Bunker Granted T2")

    InitPlayerCounter():void=
        PlayerCount := FindCreativeObjectsWithTag(RoundStartPC{})
        for (Obj : PlayerCount):
            if (PlayerCountObj := player_counter_device[Obj]):
                Print("Round Start Player Counter Found")
                PlayerCountObj.CountSucceedsEvent.Subscribe(CountSucceeded)
    
    CountSucceeded():void=
        Print("Counter Succeeded")
        DecideBunkPlayer()
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.