Verse Library verse

01 Snippet

Selects a random subset of elements from any array type using parametric typing, shuffling, and slicing.

extracted-snippets/community/snippets/AJJ/fortnite-opt-in-selection-pool-device/01-snippet.verse

# Source URL: https://dev.epicgames.com/community/snippets/AJJ/fortnite-opt-in-selection-pool-device
# Local doc:  epic-docs/community/snippets/AJJ/fortnite-opt-in-selection-pool-device.md
  using { /Fortnite.com/Characters }
  using { /Fortnite.com/Devices }
  using { /UnrealEngine.com/Temporary/Diagnostics }
  using { /Verse.org/Random }
  using { /Verse.org/Simulation }
  # Selects a random subset of Count elements from Elements and returns an array of the selected elements.
  # If Count is greater than the number of elements in Elements, all elements are returned in a random order.
  # This function uses parametric types, a feature of Verse. Parametric types allow you to specify arguments without a specific type.
  # This means that expressions can be more "generic" and operate on the arguments as long as those operations are supported by the type.
  # In this case, we don't need to know the type of each element in Elements to operate on the array, so we can use Shuffle and Slice on any type "t".
  # For more information, see https://dev.epicgames.com/documentation/en-us/uefn/Verse/parametric-types-in-verse
  SelectRandomElements<public>(Elements:[]t, Count:int where t:type):[]t=
  # Shuffle the Elements so they're in random order when we pick them.
  RandomElements := Shuffle(Elements)
  # Slice returns an array from StartIndex (first argument) to StopIndex (second argument).
  # Since the elements are in random order, this will return a random subset of Count elements from the start.
  if (Selection := RandomElements.Slice[0, Count]):
  Selection # If the Slice operation succeeds this is the last expression of the function, which is used as the implicit return.
  else:
  # If there are less than Count elements in the array, return all elements. They'll be in random order.
  RandomElements
  # This device is used by players to opt in or out of a selection pool.
  # The players interact with the OptInOutDevice to opt-in or opt-out of the SelectionPool.
  # Interacting with the SelectionTrigger device selects SelectionCount players from the pool and fires the PlayersSelectedEvent.
  opt_in_selection_pool_device := class(creative_device):
  # Used by a player to opt in or out of the selection pool.
  @editable
  OptInOutDevice:switch_device = switch_device{}

Sign in free to read the full source 🌴

This Verse Library file is members-only. Create a free account to view the complete source and download the .verse file.

Sign in with Discord

Comments

    Sign in to vote, comment, or suggest an edit. Sign in