Player Joining Gun Game

Hello! I am currently having trouble fixing uefn gun game! I have been working out a lot of bugs.

I believe I am on the right track and that it is a simple fix, I just am not experienced with verse.

Right now I am stuck on players joining and it resets the entire gun game, I fixed it slightly but I do not have a solution. I want the player to be able to join, be added to AllPlayers, and start from the first weapon without changing anyother players.

Because I need someone else to test it, it has been difficult since I need to upload it every time.

Here is my code, thank you for any help you can give!

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

# See https://dev.epicgames.com/documentation/en-us/uefn/create-your-own-device-in-verse for how to create a verse device.# A Verse-authored creative device that can be placed in a level
game_manager := class(creative_device):

    var PlayerMap : [player]int = map{}

    @editable
    var WeaponGranters : []item_granter_device = array{}

    @editable
    var Sentries : []sentry_device = array{}

    @editable
    EndGameDevice : end_game_device = end_game_device{}

    var ElimsToEndGame : int = 0

    OnBegin<override>()<suspends>:void=
        set ElimsToEndGame = WeaponGranters.Length;
        InitPlayers()
        InitTestMode()

        
    InitPlayers() : void= 
        AllPlayers := GetPlayspace().GetPlayers()

        #This is the line I added, not sure if this is in the right place.
        GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerAdded)

        for (Player : AllPlayers, FortCharacter := Player.GetFortCharacter[]):
            if (set PlayerMap[Player] = 0, WeaponTier := PlayerMap[Player]):
                FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated)
                GrantWeapon(Player, WeaponTier)

    GrantWeapon(Player : player, WeaponTier: int) : void=
        if (ItemGranter := WeaponGranters[WeaponTier]):
            ItemGranter.GrantItem(Player)

    OnPlayerEliminated(Result : elimination_result) : void=
        Eliminator := Result.EliminatingCharacter
        if (FortCharacter := Eliminator?, EliminatingAgent := FortCharacter.GetAgent[]):
            PromotePlayer(EliminatingAgent)
            


    PromotePlayer (Agent : agent) : void=
        var WeaponTier : int = 0
        if (Player := player[Agent], PlayerWeaponTier := PlayerMap[Player]):
            set WeaponTier = PlayerWeaponTier + 1
            CheckEndGame(Agent, WeaponTier)

        if (Player := player[Agent], set PlayerMap[Player] = WeaponTier):
            GrantWeapon(Player, WeaponTier)

    InitTestMode() : void=
        for (Sentry : Sentries):
            Sentry.EliminatedEvent.Subscribe(TestPlayerElimination)

    TestPlayerElimination(Agent : ?agent) : void=
        if (Player := Agent?):
            PromotePlayer(Player)

    CheckEndGame(Player : agent, WeaponTier: int) : void= 
        if (WeaponTier >= ElimsToEndGame):
            EndGameDevice.Activate(Player)

    OnPlayerAdded(InPlayer : player) : void =
        Print("Trying to add the joining player!")
        
        # Im not sure if this connects to AllPlayers in InitPlayers
        AllPlayers := GetPlayspace().GetPlayers()
     
        Print("Should be added now, or the whole game is broken!")

Hello im writing on mobile so there can be some errors. For what i can see the problem is that in initplayers you go over every player in a loop and then add each players to the player map as the key and the score 0 as the value. So for onPlayerAdded you should be able to have the same code but just not use the for loop. Here is how it could look like.

OnPlayerAdded(Player:player):void=
if:
FortCharacter := Player.GetFortCharacter
set PlayerMap[Player] = 0
WeaponTier := PlayerMap[Player]
then:
FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated)
GrantWeapon(Player, WeaponTier)

You can think the PlayerMap as the list where by putting in a player as a key you would then get that player score as the value. Key->value. So init adds all players to the map and there score and then Subscribe to OnPlayerEliminated() and grant them a weapon. So for OnPlayerAdded() we just have to do the same but only for the player that is added. For GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerAdded) you could have it in init, but would probably just add it to OnBegin instead.

1 Like

Thank you so much! I appreciate all of your help it definitely helps me understand verse a little more!

Thanks again!

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