VERSE - player Reference device - know in which device the player is registered

I’m struggling to fix the error of syntax that occure in my current code.
I want that when a player trigger a trigger device , the script check if the player is registered already in one of the 4 player_reference_device, and print it.

The error is :
This function parameter expects a value of type agent, but this argument is an incompatible value of type ?agent.

How can I resolve this?
Note : the ultimate goal will be to clear the player_reference_device corresponding to the player found.

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

# Class to handle clearing a player from the correct player reference device
player_reference_handler := class(creative_device):

    # Editable references to the player reference devices and trigger device
    @editable
    PlayerReferenceDevice1 : player_reference_device = player_reference_device{}

    @editable
    PlayerReferenceDevice2 : player_reference_device = player_reference_device{}

    @editable
    PlayerReferenceDevice3 : player_reference_device = player_reference_device{}

    @editable
    ClearTrigger : trigger_device = trigger_device{}

    # Initialize and subscribe to the trigger event
    OnBegin<override>() : void =
        # Subscribe the trigger to the Capture1 function
        ClearTrigger.TriggeredEvent.Subscribe(Capture)
        Print("Subscribed to ClearTrigger event.")

    # Functions to check if the agent is registered in each slot
    AgentIsRegistered_slot1(Agent: agent)<transacts><decides>:void =
        PlayerReferenceDevice1.IsReferenced[Agent] 

    AgentIsRegistered_slot2(Agent: agent)<transacts><decides>:void =
        PlayerReferenceDevice2.IsReferenced[Agent] 

    AgentIsRegistered_slot3(Agent: agent)<transacts><decides>:void =
       PlayerReferenceDevice2.IsReferenced[Agent] 



    # Function triggered by the trigger device to check if the agent is registered
    Capture(TheAgent: ?agent): void =

        if( AgentIsRegistered_slot1[TheAgent]):
            Print("Player referenced PlayerReferenceDevice1")
        
        if( AgentIsRegistered_slot2[TheAgent]):
            Print("Player referenced PlayerReferenceDevice2")
        
        if( AgentIsRegistered_slot2[TheAgent]):
            Print("Player referenced PlayerReferenceDevice3")

Hi. Does this help?

yes Thanks , it was related to the “?” to place at the right place

well, sadly I encounter a probably similar issue after editing the code to add some logic variables.
The goal is to check for each register device if it is empty ( no player registered in it)
and then register the player that have triggered the trigger in one of the 2 devices when the is a free slot basically .
I’m not sure if It is the appropriated manner to achieve that though, so feel free to challenge the approach.
I was hopefull there is an built in solution for checking wether or not a register device is empty but I just found this way to check if a player is registered with “isregistered”.

I would be grateful if someone can help me fixing it

below the script which have some errors :

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

# Class to handle checking registered players when a trigger is activated
player_registration_checker := class(creative_device):

    # Editable references to the player reference devices
    @editable
    PlayerReferenceDevice : player_reference_device = player_reference_device{}

    @editable
    PlayerReferenceDevice2 : player_reference_device = player_reference_device{}

    # Trigger device to detect player interactions
    @editable
    TriggerDevice : trigger_device = trigger_device{}

    # OnBegin is called when the device is initialized
    OnBegin<override>() : void =
        # Subscribe to the trigger event
        TriggerDevice.TriggeredEvent.Subscribe(OnTriggerDetected)
        Print("Waiting for a player to trigger the device...")

    # Event function called when the trigger is activated by a player
    OnTriggerDetected(TriggeringPlayer: ?agent): void =
        Print("Trigger activated by player, checking registration...")
        # Check if TriggeringPlayer exists before proceeding
        if (TriggeringPlayer?):
            CheckRegisteredPlayers(TriggeringPlayer)
   

    # Function to get the current list of players and check registration
    CheckRegisteredPlayers(TheTriggeringPlayer: ?agent)<transacts><decides> : void =
        # Get the current list of players in the game
        currentPlayers : []agent = GetPlayspace().GetPlayers()
        
        foundOnePlayer := false
        foundOnePlayer2 := false

        # Check each reference device to see if a player is registered
        for (theCurrentPlayer : currentPlayers):
            if (PlayerReferenceDevice.IsReferenced[theCurrentPlayer]):
                foundOnePlayer = true

        for (theCurrentPlayer : currentPlayers):
            if (PlayerReferenceDevice2.IsReferenced[theCurrentPlayer]):
                foundOnePlayer2 = true

        # Register the player if a device does not contain any player  registered
        if (not foundOnePlayer?):
            PlayerReferenceDevice.Register(TheTriggeringPlayer)
        else if (not foundOnePlayer2?):
            PlayerReferenceDevice2.Register(TheTriggeringPlayer)

If you plan on having more than 2 players in your map, I recommend using a map type, which can match each reference with a player. Here is documentation and a tutorial on maps:
https://dev.epicgames.com/documentation/en-us/uefn/map-in-verse
,

https://dev.epicgames.com/documentation/en-us/uefn/team-elimination-game-4-tracking-players-using-maps-in-verse

Here is an example snippet of how you would set a map up and register players:

@editable Refs : []player_reference_device = array{}
    var RefMap : [player]player_reference_device = map{}

    RegisterPlayers (): void =
        Players := GetPlayspace().GetPlayers()
        for(Index -> Player : Players):
            if(Ref := Refs[Index]):
                if(set RefMap[Player] = Ref):
                    Ref.Enable()
                    Ref.Register(Player)
                    Print("Assigned Player Ref {Index}")
        

What this does is match each player with a player reference from your Refs array, then store both of them in RefMap. Refs is an array of all your player ref devices, make sure the devices do not start enabled, so only assigned ones turn on. Now, as long as you have an Agent or Player reference, you can get RefMap[Player] and it will return the reference device that the player is registered to. Just make sure that whenever a player joins or leaves the game you call the RegisterPlayers function to update and make sure the new player gets a reference.

1 Like

Thanks for your solution! I was leaning to that way as well in the end as it allow more flexibility to collect also various infos

1 Like

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