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 | Unreal Editor for Fortnite Documentation | Epic Developer Community 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