# I have made a voting system but I need to have it randomize the candidates of the map.

**URL:** <https://forums.unrealengine.com/t/i-have-made-a-voting-system-but-i-need-to-have-it-randomize-the-candidates-of-the-map/1945266>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [July 21, 2024, 9:20pm UTC](https://forums.unrealengine.com/t/i-have-made-a-voting-system-but-i-need-to-have-it-randomize-the-candidates-of-the-map/1945266 "2024-07-21T21:20:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mtr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mtr](https://forums.unrealengine.com/u/Mtr)\
**Post date:** [July 21, 2024, 9:20pm UTC](https://forums.unrealengine.com/t/i-have-made-a-voting-system-but-i-need-to-have-it-randomize-the-candidates-of-the-map/1945266/1 "2024-07-21T21:20:35Z")

</div>

using { /Fortnite.com/Devices }  
using { /Fortnite.com/UI }  
using { /Verse.org/Simulation }  
using { /UnrealEngine.com/Temporary/Diagnostics }  
using { /Verse.org/Assets }  
using { /Verse.org/Random }  
using { /Verse.org/Colors/NamedColors }  
using { /UnrealEngine.com/Temporary/SpatialMath }  
using { /UnrealEngine.com/Temporary/UI }

# See [Create Your Own Device Using Verse in Unreal Editor for Fortnite | Fortnite Documentation | Epic Developer Community](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

S2M(S:string):message=“{S}”  
voting\_ui := class(creative\_device):

```
@editable StartVotingTrigger : trigger_device = trigger_device{}
@editable StopVotingTrigger : trigger_device = trigger_device{}

var VoteInfoPerAgent : [agent]?vote_info = map{}

@editable VotingCandidates:[]voting_candidate=array:
    voting_candidate{Texture:=Bronze,Name:="Bronze"}
    voting_candidate{Texture:=Diamond,Name:="Diamond"}
    voting_candidate{Texture:=Champion,Name:="Champion"}

# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void=
   StartVotingTrigger.TriggeredEvent.Subscribe(StartVoting)
   StopVotingTrigger.TriggeredEvent.Subscribe(StopVoting)

StartVoting(MaybeAgent:?agent):void=
    Print("Start Voting")
    Players:=GetPlayers()
    for(P:Players):
        AddUI(P)

StopVoting(MaybeAgent:?agent):void=
    Print("Stop Voting")
    WinningIndex := Tally()
    Players:=GetPlayers()
    for(P:Players):
        RemoveUI(P)
        if(set VoteInfoPerAgent[P] = false){}
    if(VC:=VotingCandidates[WinningIndex]):
        VC.Activate()

Tally():int=
    Votes:=CountVotes()
    var WinningIndexes : []int = array{}
    var WinningScore : int = 0

    for(Index -> Vote:Votes):
        if(Vote>WinningScore):
            set WinningScore=Vote
            set WinningIndexes = array{Index}
        else if(Vote = WinningScore):
            set WinningIndexes += array{Index}

    set WinningIndexes = Shuffle(WinningIndexes)

    if(WinningIndex := WinningIndexes[0]):
        return WinningIndex

    return -1

AddUI(Agent:agent):void=
    if:
        PlayerUI:=GetPlayerUI[player[Agent]]
    then:
        Canvas:=MakeCanvas(Agent)
        PlayerUI.AddWidget(Canvas,player_ui_slot{InputMode:=ui_input_mode.All})

RemoveUI(Agent:agent):void=
    if(MyVoteInfo:=VoteInfoPerAgent[Agent]?):
        if:
            PlayeUI:=GetPlayerUI[player[Agent]]
            Canvas:=MyVoteInfo.Canvas
        then:
            PlayeUI.RemoveWidget(Canvas)

CountVotes():[]int=
    Players:=GetPlayers()

    var IntergerArray : []int=array{}

    for(VC:VotingCandidates):
        set IntergerArray += array{0}

    for(P:Players):
        if(MyVoteInfo:=VoteInfoPerAgent[P]?):
            if(set IntergerArray[MyVoteInfo.Vote] += 1){}

    return IntergerArray

GetPlayers():[]player=
    return GetPlayspace().GetPlayers()

RemoveUI2(WM:widget_message):void=
    if(MyVoteInfo:=VoteInfoPerAgent[WM.Player]?):
        if:
            PlayeUI:=GetPlayerUI[player[WM.Player]]
            Canvas:=MyVoteInfo.Canvas
        then:
            PlayeUI.RemoveWidget(Canvas)

ChangeVote(WM:widget_message):void=
    Print("Change Vote")
    if(MyVoteInfo:=VoteInfoPerAgent[WM.Player]?):
        for(Index -> B:MyVoteInfo.Button):
            if(B = WM.Source):
                Print("Correct Button Found")
                set MyVoteInfo.Vote = Index
    UpdateUI()

UpdateUI():void=
    Players:=GetPlayers()
    Votes:=CountVotes()
    for(P:Players):
        if(MyVoteInfo:=VoteInfoPerAgent[P]?):
            for(Index -> B:MyVoteInfo.Button):
                if(VoteCount:=Votes[Index]):
                    B.SetText(S2M("Votes: {VoteCount}"))

MakeCanvas(Agent:agent):canvas=
    MyVoteInfo:=vote_info{}
    BackButton:=button_regular{DefaultText:=S2M("Back")}
    BackButton.OnClick().Subscribe(RemoveUI2)
    VotingStackBox:=stack_box:
        Orientation:=orientation.Horizontal

    for(VC:VotingCandidates):
        MyButton:=VC.GetButton()
        MyButton.OnClick().Subscribe(ChangeVote)
        set MyVoteInfo.Button += array{MyButton}
        MyWidget:=VC.GetStackBox(MyButton)
        MySlot:=stack_box_slot:
            Widget:=MyWidget
            Padding:=margin{Left:=90.0,Right:=90.0}
            HorizontalAlignment:=horizontal_alignment.Fill
            VerticalAlignment:=vertical_alignment.Fill
        VotingStackBox.AddWidget(MySlot)

    MyOverlay:=overlay:
        Slots:=array:
            overlay_slot:
                Widget:=texture_block{DefaultImage:=Garbage,DefaultDesiredSize:=vector2{X:=1030.0,Y:=625.0}}
                HorizontalAlignment:=horizontal_alignment.Center
                VerticalAlignment:=vertical_alignment.Center
            overlay_slot:
                Widget:=BackButton
                Padding:=margin{Left:=20.0, Bottom:=50.0}
                HorizontalAlignment:=horizontal_alignment.Center
                VerticalAlignment:=vertical_alignment.Top
            overlay_slot:
                Widget:=VotingStackBox
                Padding:=margin{Top:=80.0,Left:=20.0}
                HorizontalAlignment:=horizontal_alignment.Center
                VerticalAlignment:=vertical_alignment.Bottom
    MyCanvas:=canvas:
        Slots:=array:
            canvas_slot:
                Widget:=MyOverlay
                Anchors:=anchors{Minimum:=vector2{X:=0.5,Y:=0.5},Maximum:=Minimum:=vector2{X:=0.5,Y:=0.5}}
                Alignment:=vector2{X:=0.5,Y:=0.5}
                SizeToContent := true

    set MyVoteInfo.Canvas = MyCanvas

    if (set VoteInfoPerAgent[Agent] = option{MyVoteInfo}){}

    return MyCanvas

```

vote\_info:=class:  
var Vote : int = -1  
var Button : text\_button\_base = array{}  
var Canvas : canvas = canvas{}

voting\_candidate:=class:  
Texture:texture=Garbage  
Name:string=“”  
@editable Trigger:trigger\_device = trigger\_device{}

```
Activate():void=
    Trigger.Trigger()

GetButton():button_loud=
    return button_loud{DefaultText:=S2M("Vote")}

GetStackBox(MyButton:button_loud):stack_box=
    return stack_box:
        Orientation:=orientation.Vertical
        Slots:=array:
            stack_box_slot:
                Widget:=texture_block:
                 DefaultImage:=Texture
                 DefaultDesiredSize:=vector2{X:=200.0,Y:=200.0}
                HorizontalAlignment:=horizontal_alignment.Center
                VerticalAlignment:=vertical_alignment.Center
            stack_box_slot:
                Widget:=text_block:
                    DefaultText:=S2M("{Name}")
                    DefaultTextColor:= White
                HorizontalAlignment:=horizontal_alignment.Center
                 VerticalAlignment:=vertical_alignment.Center
                 Padding:=margin{Top:=10.0,Bottom:=10.0}
            stack_box_slot:
                Widget:=MyButton
                HorizontalAlignment:=horizontal_alignment.Center
                 VerticalAlignment:=vertical_alignment.Center

```

---

<div class="post-metadata">

**Author:** ![Trance\_Fury\_YT](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/trance_fury_yt/32/887649_2.png) [@Trance\_Fury\_YT](https://forums.unrealengine.com/u/Trance_Fury_YT)\
**Post date:** [July 22, 2024, 2:12am UTC](https://forums.unrealengine.com/t/i-have-made-a-voting-system-but-i-need-to-have-it-randomize-the-candidates-of-the-map/1945266/2 "2024-07-22T02:12:33Z")

</div>

Can you elaborate a little more on randomizing the candidates? Do you mean you need to pick 3 random players for voting?

There are a number of ways you can do this. One way I can think of is to get the Player array and use the

` Random.GetRandomInt(0, Players.Length -1)`

3 different times. Then just grab the player from the array with the random number.
