I have been having a lot of fun learning verse this week! I am working on a few things I hope to share soon, but for now I wanted to make something that others might find useful to use or learn from.
Here is a simple voting system
My version below has 3 vote/inputs with the end result of being teleported, in a game you could use this to choose between 3 areas/games between. In the event of a 2 way draw, the lowest of the 3 options is eliminated and the vote re-starts. In the event of a 3 way draw a random option is chosen.
I would love any feedback or advice on making this more efficient, or perhaps you spotted an edge case I had not yet considered? Any and all feedback welcome and appreciated
using { /Verse.org/Native }
using { /Verse.org/Verse }
using { /Verse.org/Simulation }
using { /Fortnite.com/Devices}
using { /EpicGames.com/Temporary/Diagnostics }
using { /Verse.org/Random }
<# ===
UEFN Voting Script Version 1.0 by Kyle Boswell :)
Epic: Kyle Dev
Twitter: @Dev__Kyle
=== #>
log_vote_device:=class(log_channel){}
vote_device := class<concrete>(creative_device):
Logger:log = log{Channel:=log_vote_device}
# Devices:
# (Buttons for voting)
@editable
Button_A:button_device = button_device{} # link button = Button_A
@editable
Button_B:button_device = button_device{} # link button = Button_B
@editable
Button_C:button_device = button_device{} # link button = Button_C
@editable
StartVote:button_device = button_device{} # link button = StartVote
@editable
VoteTimeDevice:timer_device = timer_device{} # link timer device = VoteTimeDevice
# (Teleport to here when vote win)
@editable
TeleA:teleporter_device = teleporter_device{}
@editable
TeleB:teleporter_device = teleporter_device{}
@editable
TeleC:teleporter_device = teleporter_device{}
# (Lights to visually see active votes)
@editable
LightA:customizable_light_device = customizable_light_device{} # link button = light A
@editable
LightB:customizable_light_device = customizable_light_device{} # link button = light B
@editable
LightC:customizable_light_device = customizable_light_device{} # link button = light C
# Variables:
var randomNumber: int = 0 # Var to hold random number
# (Hold the totals of the vote)
var totalA : int = 0
var totalB : int = 0
var totalC : int = 0
# (True if draw betwwen 2 options)
var drawAB : logic = false
var drawBC : logic = false
var drawCA : logic = false
# (True if A B or C win)
var winA : logic = false
var winB : logic = false
var winC : logic = false
# FUNCTION: OnBegin - This will start at the start of the game.
OnBegin<override>()<suspends>:void=
Button_A.InteractedWithEvent.Subscribe(VotedA) # if player interacts with button A then run VotedA
Button_B.InteractedWithEvent.Subscribe(VotedB) # if player interacts with button B then run VotedB
Button_C.InteractedWithEvent.Subscribe(VotedC) # if player interacts with button A then run VotedC
StartVote.InteractedWithEvent.Subscribe(VoteTimer) # if player interacts with StartVote button then run VoteTimer
# FUNCTION: Voted A - This code will run when Button_A is interacted with by the player raising the total A count by 1.
VotedA(Player:player):void=
Logger.Print("=== VotedA Function Activated ===")
set totalA = totalA + 1; # Add 1 to the total of A
Logger.Print("=== | A = {totalA} | B = {totalB} | C = {totalC} | ===") # Write total to log
# FUNCTION: Voted B - This code will run when Button_B is interacted with by the player raising the total B count by 1.
VotedB(Player:player):void=
Logger.Print("=== VotedB Function Activated ===")
set totalB = totalB + 1; # Add 1 to the total of B
Logger.Print("=== | A = {totalA} | B = {totalB} | C = {totalC} | ===") # Write total to log
# FUNCTION: Voted C - This code will run when Button_C is interacted with by the player raising the total C count by 1.
VotedC(Player:player):void=
Logger.Print("=== VotedC Function Activated ===")
set totalC = totalC + 1; # Add 1 to the total of C
Logger.Print("=== | A = {totalA} | B = {totalB} | C = {totalC} | ===") # Write total to log
# FUNCTION: Vote timer - This is a timer... when the timer ends it will run VoteResults()
VoteTimer(Player:player):void=
Logger.Print("=== VoteTimer Function Activated ===")
VoteTimeDevice.Enable # Enable timer
VoteTimeDevice.Start # Start timer
VoteTimeDevice.SuccessEvent.Subscribe(VoteResults) # When timer ends, run the function VoteResults
# FUNCTION: Vote results - This will calculate which vote is highest or if there is a draw or if there is no votes.
VoteResults(Player:player):void=
Logger.Print("=== VoteResults Function Activated ===")
# Check if 3 way draw (or no one voted)
if (totalA = totalB && totalB = totalC):
Logger.Print("=== Its a 3 way draw ===")
GetRandom(Player) # choose a random result
# Check if 2 way draws
else if ( totalA = totalB && totalA > totalC && totalB > totalC): # if A and B are equal, and A and B are higher than C
Logger.Print("=== Its a draw between A and B ===")
set drawAB = true # Confirm A and B is draw
DrawVote(Player) # Run DrawVote Function
else if ( totalB = totalC && totalB > totalA && totalC > totalA): # if B and C are equal, and B and C are higher than A
Logger.Print("=== Its a draw between B and C ===")
set drawBC = true # Confirm B and C is draw
DrawVote(Player) # Run DrawVote Function
else if ( totalC = totalA && totalC > totalB && totalA > totalB): # if C and A are equal, and C and A are higher than B
Logger.Print("=== Its a draw between C and A ===")
set drawCA = true # Confirm C and A is draw
DrawVote(Player) # Run DrawVote Function
# Check if wins
else if ( totalA > totalB && totalA = totalC): # if A is highest
Logger.Print("=== A is highest ===")
set winA = true # set variable winA to true
Logger.Print("=== Winner A ===")
TeleA.Teleport(Player) # teleport players to TeleA
DrawVote(Player) # run DrawVote to reset totals and timer
else if ( totalB > totalA && totalB > totalC): # if B is highest
Logger.Print("=== B is highest ===")
set winB = true # set variable winB to true
Logger.Print("=== Winner B ===")
TeleB.Teleport(Player) # teleport players to TeleB
DrawVote(Player) # run DrawVote to reset totals and timer
else if ( totalC > totalB && totalC > totalA): # if C is highest
Logger.Print("=== C is highest ===")
set winC = true # set variable winC to true
Logger.Print("=== Winner C ===")
TeleC.Teleport(Player) # teleport players to TeleB
DrawVote(Player) # run DrawVote to reset totals and timer
# FUNCTION: DrawVote - This runs if there is a draw in the vote, the code resets the vote and the disables the lowest result so you can vote again between the two draw winners.
DrawVote(Player:player):void=
Logger.Print("=== DrawVote Function Activated ===")
# Reset totals to 0
set totalA = 0;
set totalB = 0;
set totalC = 0;
# If draw, disable the 3rd (lowest) option
if (drawAB = true): # if A B draw
Button_C.Disable() # Disable button C
LightC.TurnOff()
Logger.Print("=== Button C disabled ===")
else if (drawBC = true): # if B C draw
Button_A.Disable() # Disable button A
LightA.TurnOff()
Logger.Print("=== Button A disabled ===")
else if (drawCA = true): # if C A draw
Button_B.Disable() # Disable button B
LightB.TurnOff()
Logger.Print("=== Button B disabled ===")
VoteTimer(Player) # Restart the vote timer
# FUNCTION: Choose random - This generates a random number betweeen 1 and 3 if there is a 3 way draw of if no one votes.
GetRandom(Player:player):void=
Logger.Print("=== GetRandom Function Activated ===")
RandomNum:int = GetRandomInt(1, 3)
Logger.Print("Picked Random Number. Number selected is {RandomNum}")
set randomNumber = RandomNum
if (randomNumber = 1):
set winA = true
TeleA.Teleport(Player)
Logger.Print("=== Winner A ===")
if (randomNumber = 2):
set winB = true
TeleB.Teleport(Player)
Logger.Print("=== Winner Bb ===")
if (randomNumber = 3):
set winC = true
TeleC.Teleport(Player)
Logger.Print("=== Winner C ===")
<# ===
UEFN Voting Script Version 1.0 by Kyle Boswell :)
Epic: Kyle Dev
Twitter: @Dev__Kyle
=== #>