How I can create a player instance?

Hi everyone!

I’m trying to instantiate player as follows:

Player1 : player = player{}

var Player2 : player = player{}

But, I get this error:

Invalid access of epic_internal class constructor (/Verse.org/Simulation:)player from class /localhost/PlatformingGame/game_manager

You can’t construct these internal types. Why would you want to create a player though?

I have a map with players(key) and score(value). So I want to iterate the map and get the least value and the player. After iterating (outside the map), I want to use the player on the end device. But the player is within the range of the map.

    var GreaterNumber : int = 0
    for (Key->Value : PlayerMap):
        if (Value > GreaterNumber):
            set GreaterNumber = Value
            # Get and store the Key (Player)
    EndDevice.Activate(Player)

I think when you use a player as a key ina map, internally the engine is doing some magic and I’m guessing it hashes the player struct/object as a unique identifier and stores it as a reference? Cause it won’t store the whole player as a key I wouldn’t have thought (any staff confirmations/explanations on this would be good knowledge while I’m learning by the way).

Which means Key is that unique identifier, not the player, so you might be able to do something like for (Key -> Value: PlayerMap, Player := player[Key]), which will give you a Player variable.

This probably won’t work though as the player[Key] is for checking if an Agent is a Player usually, but it might be able to do the same with the unique identifier key too, worth a try at least as internally it might be able to map the references/unique identifiers the same way.

You could also try just passing the Key to the Activate method on EndDevice like so: EndDevice.Active(Key), I’m pretty confident that won’t work either but worth just marking it off the list of possibilities to help find the answer.

The Verse Elimination Template Example includes usage of a player/int map. 4. Tracking Players Using Maps

Something like below should provide a solid base to expand from.

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

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

    # Map container to track an int value per player
    var AgentMap : [agent]int = map{}

    OnBegin<override>()<suspends>:void=
        AllPlayers := GetPlayspace().GetPlayers() <# Get all the players in the experience #>
        for (Player : AllPlayers): <# For every player #>
            if (FortCharacter := Player.GetFortCharacter[]): <# Make sure its a fortnite player character #>
                FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated) <# subscribe to eliminated event #>
                if (set AgentMap[Player] = 0) {} <# and then add the player to our map #>

    # Event that handles when a player is eliminated
    OnPlayerEliminated(Result:elimination_result):void=
        Print("Player Eliminated")
        EliminatingCharacter := Result.EliminatingCharacter <# Get the damage causer out of the elimination result #>
        if (FortCharacter := EliminatingCharacter?): <# Make sure its a fortnite player character #>
            if (EliminatingAgent := FortCharacter.GetAgent[]): <# Get the agent out of the fortnite player character #>
                if (var CurrentValue:int = AgentMap[EliminatingAgent]): <# Get the existing value out of the map #>
                    set CurrentValue = CurrentValue + 1 <# Incriment the value #>
                    if (set AgentMap[EliminatingAgent] = CurrentValue) {} <# Update the value in the map #>

    # Return the player with the lowest value in the map
    GetLowestValuedPlayer():?agent=
        var FoundValue:int=1000
        var FoundAgent:?agent=false
        for (Key->Value : AgentMap):
            if (Value < FoundValue):
                set FoundValue = Value
                set FoundAgent = option{Key}
        return FoundAgent

    # Return the player with the Highest value in the map
    GetHighestValuedPlayer():?agent=
        var FoundValue:int=0
        var FoundAgent:?agent=false
        for (Key->Value : AgentMap):
            if (Value > FoundValue):
                set FoundValue = Value
                set FoundAgent = option{Key}
        return FoundAgent
2 Likes

It has been successful! It is what I want to do!!

    var FoundValue : int = 0
    var FoundPlayer : ?player = false
    for (Key->Value: PlayerMap):
        if (Value >= FoundValue):
            set FoundValue = Value
            set FoundPlayer = option{Key}
    if (Player := FoundPlayer?):
        EndDevice.Activate(Player)

But, I have a question. If I want to use the function as follow:

    CheckEndGame()  : void=
           if (Player := GetLowestValuedPlayer()?):

I have the following error:

This invocation calls a function that has the ‘no_rollback’ effect, which is not allowed by its context.

@JHBOGameDev Store the result of GetLowestValuedPlayer in a variable first and then do the if statement.