Murder Mystery Style Random Selector

Hi, So I’m making a murder mystery type game, can someone help me with the code or how to do this system in verse to select 2 imposters out of all the players (Target 10, Could be any amount) and for now just use a message ui device or whaterver (HUD message Devide). Something like in the fortnite maps “Murder Mystery”

https://www.fortnite.com/@goodgamers/5253-8468-3364?lang=en-US

Thank you

Hello @YourCooked how are you?

Let’s start with this!

First of all, you will need to implement the functions to send the players to the corresponding teams. We will use Team 1 for the Survivors and Team 2 for the Murderers. One thing you need to know about teams, is that they have an index in an array of teams, so the Team 1 is Index 0 and the Team 2 is Index 1. I have comments to help you with that in my code, don’t worry.

What we will do, is to get all the players on the Island, create an array with them and then shuffle that array and store it in a different one. After that, we will send the first 2 players in that last array to the Team 2 (Index 1 - Murderers team) and the rest of the players to Team 1 (Index 0 - Survivors team).

This is the code for that:

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

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

    var Murderers : int = 2
    var PlayersArray : []player = array{}
    var MurderersArray : []player = array{}

    # Function to change all the players to the corresponding team
    TeamAssignment() : void =
        # Get all players in the island and store them in AllPlayers array
        AllPlayers := GetPlayspace().GetPlayers()
        # Shuffle the previous array and store the new order in PlayersArray
        set PlayersArray = Random.Shuffle(AllPlayers)
        # Iterate all the players in PlayersArray to assign them to a team
        for ( Index := 0..PlayersArray.Length - 1 ):
            if (Index <= Murderers - 1): # Assign only the first X players in the array to Murderers Team, where X is the Murderers variable
                if ( Player := PlayersArray[Index] ):
                    # Send player to team index 1 (Team 2 - Murderers)
                    ChangePlayerTeam(Player, 1)
                    if(set MurderersArray[Index] = Player):
            else: # Send the rest of the players to Survivors Team
                if ( Player := PlayersArray[Index] ):
                    # Send player to team index 0 (Team 1 - Survivors)
                    ChangePlayerTeam(Player, 0)


    # Function to change a Player's team
    ChangePlayerTeam(Player : player, NewTeamIndex : int) : void =
        if:
            # Get all the teams on the island
            TeamCollection := GetPlayspace().GetTeamCollection()
            Teams := TeamCollection.GetTeams()

            # Check if the team we are trying to assing the players exists
            NewTeamIndex >= 0
            NewTeamIndex < Teams.Length

            # Set the new team we will assign to the player
            NewTeam := Teams[NewTeamIndex]
        then:
            # Assign the player to the new team
            if(TeamCollection.AddToTeam[Player, NewTeam]):

Keep in mind that you can change the amount of Murderers for your game by setting a different number in the “Murderers” variable. And the total number of players doesn’t matter for this code, as it uses all the players on the island!

Ok, now we want to show who were the murderers at the end of the match! For that, we will need to create a Widget Blueprint and link it to a HUD Message Device!

To create a widget blueprint you need to follow this steps:

  1. Right click on your content browser, search for Widget Blueprint and click on it.

  2. Choose “User Widget” on the next screen.

  3. Name your new file something like “WBP_Murderers” (WBP is the prefix for Widget Blueprint), open the BP, search for “Canvas” in the “Palette” section, and drag the canvas to the grid.

  4. Then do the same with a Text Block.

You will end with something like this:

Ok, now we need to setup our widget to be able to receive info from a HUD Message Device. To do that you need to follow this steps:

  1. Go to the bottom left corner of the Widget screen and click on View Bindings

  2. Then click on Add Viewmodel.

  3. Choose “Device - Message View Model”.

  4. Close the next window.

  5. Click again on View Bindings and then click on Add Widget Text Block.

  6. Then click on Text Block.
    image

  7. Select “Text”.

  8. Click on “No field selected”.

  9. Choose “MVVM_UEFN_HUDMessage” and then “Text”.

  10. Compile and save your widget blueprint.

Now your widget is ready to receive data from a HUD Message Device. Place the device anywhere on your island and search for the field “HUD Widget” in its “Details” tab. Then choose your Widget Blueprint on the list:

The last thing we need to do is to set the name of the Murderers as the text for that HUD Device. We will do it using Verse.

Fist we need to create a “message” variable that will contain the names of the murderers and some extra text if you want. Then we will ned a variable to store the HUD Message Device. And lastly, we will need a function to update and show the text.

This is the code:

  1. Add these variables before the “OnBegin” function
    @editable # HUD device to show the message to all players
    var HudDevice: hud_message_device = hud_message_device{}
    # Define the message to show, you will need to modify the amount of variables in this Message if you change the amount of Murderers
    KillstreakText<localizes>(Murderer1: agent, Murderer2: agent): message = "{Murderer1} and {Murderer2} were the Murderers!"
  1. Add this function to the code I shared with you earlier on this post
    # This function shows who were the Murderers on screen
    ShowMurderers(): void =
        # You will need to modify the amount of this variables if you change the amount of Murderers
        if (FirstMurderer := MurderersArray[0]):
            if (SecondMurderer := MurderersArray[1]):
                # this set the Murderer Players in the text, and sends it to the HUD Device designated Widget
                # You will need to modify the amount of variables in this Message if you change the amount of Murderers
                HudDevice.SetText(KillstreakText(FirstMurderer, SecondMurderer))
                # this gives the order to the HUD Device to be shown to all players on the Island
                HudDevice.Show()

The only thing you need to do now is compile and save your code, and add your HUD Device to your new Custom device in your Island.

And that’s it! Now you can trigger those functions whenever you want.

If you want to customize your widget to look better, I recomend you to watch this video

Hope this helps you!

1 Like

Thank you so much. i really appreciate it.