Sets

A set is another type of container. It is an unordered list of unique elements. The search for an element of the set is based on the value of the element itself. There is no index. The elements of a set must be of the same type, and repeated elements are not allowed.

To create a set, first create a variable and define its type. Then, click the icon next to the Variable Type drop-down menu and select the icon of a set, as shown in this screenshot:

Compile the Blueprint so that you can add default values to the set. In the Default Value panel of the variable, click the + icon to add elements to the set. The following screenshot shows an example of a String set with four elements:

The next screenshot shows some of the Actions of the set container. Here is a brief description of each Action:

  • ADD: Adds an element to a set.
  • ADD ITEMS: Adds elements from an array to a set. The array must be of the same type as the set.
  • CONTAINS: Returns true if the set contains the element.
  • LENGTH: Returns the number of elements in a set:

A set does not have a GET element node, so if you need to iterate through the elements of a set, then you can copy the elements of a set to an array. The next screenshot shows the TO ARRAY node and other nodes that are used to remove elements:

  • TO ARRAY: Copies the elements of a set to an array
  • CLEAR: Removes all elements of a set
  • REMOVE: Removes an element of a set, or returns True if an element was removed and False if the element was not found
  • REMOVE ITEMS: Removes the elements specified in an array from a set:

Some nodes perform operations with two sets and return a different set. These nodes are shown in the next screenshot:

  • UNION: The resulting set contains all the elements from the two sets. Since the result is a set, all duplicates will be removed.
  • DIFFERENCE: The result set contains the elements of the first set that are not in the second set.
  • INTERSECTION: The result set contains the elements that exist in the two sets:

The next screenshot shows a simple example of the use of a set. There is a set named Unique Names that keeps the names of the players who have won a round. In this case, we want to know the players who have won at least one round. We do not need to know how many rounds a player has won:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset