how can i make these two verse code to work

can any help me make these two verse code work together, its a spectator verse where you can respawn and the other is a delay respawn verse. but in the spectator verse the player will not being able to spectate the other players becouse of the delay verse, so can you help so that a player can spectate other and not just respawn. and so that you Can only interact with the spectator button if there is 2 players in the game (if its possible)

SPECTATE BUTTON with RESPAWN

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Fortnite.com/Characters }
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /Fortnite.com/FortPlayerUtilities }

PlayerUIClass := class():

RespawnClass : class_and_team_selector_device
RespawnText<localizes> : message = "Respawn"
ButtonRespawn : button_loud = button_loud{}
var MyCanvas : canvas = canvas{}


OnRespawnClicked(WidgetPlayer:widget_message):void=
    if(Player:= WidgetPlayer.Player, PlayerUI:= GetPlayerUI[Player]):
            PlayerUI.RemoveWidget(MyCanvas)
            RespawnClass.ChangeClass(Player)
            Player.Respawn(vector3{X:=1.0,Y:=1.0,Z:=1.0}, IdentityRotation())
            

Init() : canvas =
    
    ButtonRespawn.SetText(RespawnText)

    ButtonRespawn.OnClick().Subscribe(OnRespawnClicked)
    
    MainCanvas : canvas = canvas:
        Slots := array:
            canvas_slot:
                Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.2}, Maximum := vector2{X := 0.5, Y := 0.2}}
                Offsets := margin{Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0}
                Alignment := vector2{X := 0.5, Y := 0.5}
                SizeToContent := true
                Widget := ButtonRespawn

    return MainCanvas

AddUI(Agent : agent) : void=
    if(Player := player[Agent], PlayerUI := GetPlayerUI[Player]):

        set MyCanvas = Init()
        PlayerUI.AddWidget(MyCanvas , player_ui_slot{InputMode := ui_input_mode.All})

Instanr Respawn with Delay trigger

@editable SpectateButton:button_device = button_device{}
@editable SpectatorClass:class_and_team_selector_device = class_and_team_selector_device{}
@editable RespawnClass:class_and_team_selector_device = class_and_team_selector_device{}


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

    SpectateButton.InteractedWithEvent.Subscribe(GoToSpectator)

GoToSpectator(Agent:agent):void=
    if(Player:= player[Agent], PlayerUI:=GetPlayerUI[Player]):
        if(FortChar := Player.GetFortCharacter[]):
            SpectatorClass.ChangeClass(Agent)
            FortChar.Damage(300.0)

            EachPlayerUIClass: PlayerUIClass = PlayerUIClass{RespawnClass:=RespawnClass}
            EachPlayerUIClass.AddUI(Agent)

discord.gg/uefndream

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

InstantRespawn := class(creative_device):

respawnposition: vector3=vector3{X:= 250.00, Y:= 0.01, Z:= 161.9}

@editable
var RespawnTime : float = 0.1

@editable
FastRespawnTrigger : trigger_device = trigger_device{}

@editable
SlowRespawnTrigger : trigger_device = trigger_device{}

@editable
ElimMachine : elimination_manager_device = elimination_manager_device{}
@editable
TeleporterTeam1 : teleporter_device = teleporter_device{}
@editable
TeleporterTeam2 : teleporter_device = teleporter_device{}

NowRespawn(Agent: agent)<suspends>:void=
    Sleep(RespawnTime)
    Agent.Respawn(respawnposition, rotation{})
    TeleporterTeam1.Teleport(Agent)
    TeleporterTeam2.Teleport(Agent)

SelfEliminated(Agent:agent):void=
    spawn{NowRespawn(Agent)}

OnFastRespawnTriggered(Agent:?agent):void=
    set RespawnTime = 0.1

OnSlowRespawnTriggered(Agent:?agent):void=
    set RespawnTime = 1.5

OnBegin<override>()<suspends>:void=
    ElimMachine.EliminatedEvent.Subscribe(SelfEliminated)
    FastRespawnTrigger.TriggeredEvent.Subscribe(OnFastRespawnTriggered)
    SlowRespawnTrigger.TriggeredEvent.Subscribe(OnSlowRespawnTriggered)

:grinning_face:

@BRGJuanCruzMK

Are you here I relly need youre help

@KongJoh how is it going?

Sorry for the delay! I’m working on this and I’ll contact you again once I have a proper answer :wink:

1 Like

Hey @BRGJuanCruzMK i doing good thanks. How about You?

Its nice to hear that You are helping me thank You so much :grinning_face:

I’m trying to figure out what is the result you are expecting from this code to proceed with a solution. So let me ask some questions:

  1. You want two different respawn times based on a trigger, right?

  2. While a player is dead, it should be able to expectate the other players, right?

  3. If a player is spectating, it should have a button to respawn after the corresponding time (based on question 1), right? I mean, it should not respawn if it didn’t press the button.

Based only in assumptions, i guess what you want is: if a player dies, it becomes a spectator during the corresponding time (01 or 1.5 secs), after that time, the player should get a “respawn” button to go back to the fight, but NOT respawn automatically. Is that correct?

In addition to these answers, I’d like to see the config for the Class Selectors you are using for this.

  1. i have the different respawn times but its becouse the spectator verse code dont work with the respawn times, i dont know why but every time i use the button for the spectator verse there will only come a ui button thats saying respawn but the player are not spectating why. but when i press the ui respawn button the player will spawn in a new location, and not on the playerspawn

2 yes

3 yes

4 if a player dies, it becomes a spectator during the corresponding time (01 or 1.5 secs), after that time, the player should get a “respawn” button to go back to the fight, but NOT respawn automatically, yes but no. the player will spectate when interact with the button in the spectate verse, and yes i want so it NOT respawn automatically

thanks for the help :slight_smile:

Ok, I think I have a solution for this!

First of all, you will need to set your Island Settings this way:

After that, you will need to implement this Class (Verse Code)

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Fortnite.com/Characters }
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /Fortnite.com/FortPlayerUtilities }

# UI class for handling player respawn interface
PlayerUIClass := class():
    # Device used to change player class upon respawn
    RespawnClass : class_and_team_selector_device
    # Localized text for the respawn button
    RespawnText<localizes> : message = "Respawn"
    # Button widget for triggering respawn action
    ButtonRespawn : button_loud = button_loud{}
    # Canvas container for the UI elements
    var MyCanvas : canvas = canvas{}
    # Location where the player will respawn
    var SpawnLocation : vector3 = vector3{}

    
    # Event handler called when the respawn button is clicked
    OnRespawnClicked(WidgetPlayer:widget_message):void=
            if(Player:= WidgetPlayer.Player, PlayerUI:= GetPlayerUI[Player]):
                    # Remove the respawn UI from the screen
                    PlayerUI.RemoveWidget(MyCanvas)
                    # Change the player to the respawn class
                    RespawnClass.ChangeClass(Player)
                    # Respawn the player at the specified location with default rotation
                    Player.Respawn(SpawnLocation, IdentityRotation())

    # Initialize and create the UI canvas with respawn button            
    Init() : canvas =
        # Set the button text to the localized respawn message
        ButtonRespawn.SetText(RespawnText)
        # Subscribe to button click events
        ButtonRespawn.OnClick().Subscribe(OnRespawnClicked)
        # Create the main canvas container
        MainCanvas : canvas = canvas:
            Slots := array:
                canvas_slot:
                    # Center the button horizontally and position it at 20% from the top
                    Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.2}, Maximum := vector2{X := 0.5, Y := 0.2}}
                    # No additional margins around the button
                    Offsets := margin{Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0}
                    # Center alignment for the widget within its slot
                    Alignment := vector2{X := 0.5, Y := 0.5}
                    # Automatically size the slot to fit the button content
                    SizeToContent := true
                    # Assign the respawn button as the widget for this slot
                    Widget := ButtonRespawn
        return MainCanvas

    # Add the respawn UI to a specific player's screen                
    AddUI(Agent : agent, Location : vector3) : void=
        if(Player := player[Agent], PlayerUI := GetPlayerUI[Player]):
            # Store the spawn location for when respawn is triggered
            set SpawnLocation = Location
            # Initialize the UI canvas
            set MyCanvas = Init()
            # Add the canvas to the player's UI with full input mode enabled
            PlayerUI.AddWidget(MyCanvas , player_ui_slot{InputMode := ui_input_mode.All})

Once you do that, you will need to create this device:


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

# Respawn system with automatic spectator mode
respawn_with_delay := class(creative_device):

    # Editable device for spectator class selection
    @editable 
    SpectatorClass:class_and_team_selector_device = class_and_team_selector_device{}
    
    # Editable device for respawn class selection
    @editable 
    RespawnClass:class_and_team_selector_device = class_and_team_selector_device{}
    
    # Time in spectator mode before respawn (in seconds)
    @editable
    RespawnDelay:float = 5.0

    # Array of player spawner devices for spawn locations
    @editable
    var PlayerSpawners : []player_spawner_device = array{}
    # Variable to store the selected spawn location
    var SpawnLocation : vector3 = vector3{}

    # Map to track players currently in spectator mode
    var PlayersInSpectator: [agent]logic = map{}

    OnBegin<override>()<suspends>:void=
        # Get all players in the game and subscribe to their death events
        AllPlayers := GetPlayspace().GetPlayers()
        for (Player : AllPlayers):
            if (Agent := agent[Player]):
                SubscribeToPlayerEvents(Agent)


    # Subscribe to events for a specific player
    SubscribeToPlayerEvents(Agent:agent):void=
        if (Player := player[Agent]):
            if (FortChar := Player.GetFortCharacter[]):
                # Subscribe to the elimination event for this character
                FortChar.EliminatedEvent().Subscribe(OnPlayerEliminated)


    # Event handler called when a player is eliminated
    OnPlayerEliminated(Result:elimination_result):void=
        EliminatedChar := Result.EliminatedCharacter
        if (Agent := EliminatedChar.GetAgent[]):
            # Send the eliminated player to spectator mode
            GoToSpectator(Agent)


    # Main function to switch player to spectator mode
    GoToSpectator(Agent:agent):void=
        if (Player := player[Agent]):
            # Mark player as being in spectator mode
            if( set PlayersInSpectator[Agent] = true):
            
            # Change player to spectator class
            SpectatorClass.ChangeClass(Agent)
            
            # Start the respawn delay timer in a separate thread
            spawn { HandleRespawnDelay(Agent) }

    # Handle the delay before showing respawn UI        
    HandleRespawnDelay(Agent:agent)<suspends>:void=
        Sleep(RespawnDelay)
        ShowRespawnUI(Agent)
    

    # Function to get a random spawn location from available spawners
    GetRandomSpawnLocation():void = 
        # Check if there are any spawners available
        if (PlayerSpawners.Length > 0):
            # Get a random index within the spawner array bounds
            RandomIndex := GetRandomInt(0, PlayerSpawners.Length - 1)
            if (RandomSpawner := PlayerSpawners[RandomIndex]):
                # Get the spawner's position
                set SpawnLocation = RandomSpawner.GetTransform().Translation
                # Add height offset to spawn above ground level
                set SpawnLocation = SpawnLocation + vector3{X := 0.0, Y := 0.0, Z := 120.0}
        # Use default location if no spawners are configured (you can change this as you wish)
        else:
            set SpawnLocation = vector3{X := 0.0, Y := 0.0, Z := 0.0}


    # Display the respawn UI to the player
    ShowRespawnUI(Agent:agent):void=
        # Check if player is still marked as in spectator mode
        if (PlayersInSpectator[Agent]?):
            # Get a random spawn location for respawn
            GetRandomSpawnLocation()
            # Create and show custom UI for the player
            if (Player := player[Agent], PlayerUI := GetPlayerUI[Player]):
                # Create UI class instance with respawn configuration
                EachPlayerUIClass: PlayerUIClass = PlayerUIClass{RespawnClass := RespawnClass}
                # Add the UI to the player with the spawn location
                EachPlayerUIClass.AddUI(Agent , SpawnLocation)

The last step is to drop your new device to your Island and add the reference devices to it like this:

You will need to add here ALL the Player Spawner devices you want to use as spawn points, or you can replace the Player Spawners for any other device to use as reference. You only need to change the variable type in the verse code!

With that being said, this should work as you want!

Hope it helps!

sorry for not replying to you but thank you so much for the help, you are the best :slight_smile:

1 Like