• Hey! Register here to create your account, engage with the community, and talk about what is new!

Thorough Guide to Set Variable: List Manipulation

RDM

Member
Overlord
Joined
Sep 6, 2020
Messages
36
Reaction score
22
Hi y'all. Lists are an important feature on DiamondFire, making the hard easy and the impossible possible. If you're having a tough time understanding how to use lists or where you can use them, this is the guide for you. Feel free to reply to this guide with any suggestions, comments, questions, or concerns you may have.

Part 1: How to use Lists

1601072212154.png

Lists are a part of the Set Variable code block, available within the Set Variable categories GUI (see above). A list in coding terms is basically the equivalent to an array in java. Lists are a collection of different items, comparable to a grocery list. It's a little bit different in coding, but the premise is still the same. Located below is an explanation on how lists work. Note: I will be describing lists as per how they work in DiamondFire, not in java. If you're seeking an arrays tutorial in java, I recommend visiting here.

You will always have a variable representing your lists as you create them. This is the 'list variable,' the variable containing the entirety of your list.

1601073756346.png

To add values to your list, you'll usually append a value to your list. Append just means add it to the bottom. :smile:
(Note: Values aren't just numbers, they can also be text, item variables, sounds, potions, colors hexes, locations, particles, variables, or game values!)

1601074292630.png

All of your list values will be stored in your list variable, organized into neat little pieces called indexes. Indexes are just numbers used to organize all your list values and are entirely separate from your actual values. When you append values into your list, this index number goes up from zero (the first index would be 1). This enables you to do a lot with lists, including the sorting of your values, obtaining specific values from your list, and even inserting values in between specific indexes.

Note: All lists have completely separate indexes. Every list that is created will have its indexes start from zero.

Part 2: How to apply Lists

There are a lot of different ways you can apply lists. Here are the ones within the Set Variable code action with provided explanations.

Create List: Creates a list. Values are optional, so you could create a list with no values in it if you wanted (a blank list).

Append Value: Adds a value to the end of a list. A new index will be created (so if your previous value had an index of 2, this next value will have an index of 3)

Append List: Adds another list to the end of a list. Basically it just combines the lists and you'll find the other lists' indexes at the end of the first one.

Get List Value at Index: Obtains a list value at an index.

Set List Value at Index: Replaces a list value with something else at an index.

Get List Index of Value: It's the opposite of Get List Value at Index. Obtains the index of a list value. Returns 0 if the value is not found in the list.

Get List Length: Obtains the amount of indexes a list has.

Insert Value at Index: Creates a new value at a certain index. The indexes change accordingly. For example, here's a list: A (1) B (2) C (3). If I added value X into this list at index 2, the list would now look like A (1) X (2) B (3) C (4).

Remove Value at Index: Removes a value at a certain index. The indexes change accordingly. Similar example to Insert Value at Index except it's backwards.

Remove List Value: Removes all matching values. Indexes update accordingly.

Trim List: Trims the list, similar to Set Variable: Trim Text, only with indexes instead of characters. Input a start index and an end index; all indexes outside that range will be removed. Indexes change accordingly. If the end index is too large (i.e. if your list contains 20 indexes and your end index is 50) it will be trimmed up to the last index instead.

Sort List: Sorts a list's values in ascending or descending order. Only works with numbers, texts, or sub-lists (lists inside of lists). Indexes update accordingly.

Reverse List: Flips the order of a list's values, making values at the back switch places with values in the front. Indexes update accordingly.

Randomize List: Randomizes the order of a list's values. Indexes update accordingly.

1601078794677.png

Part 3: Extras

Here are the extra list features not located in Set Variable.

Game Value: There are numerous game values that have returns in the form of lists. Here are these game values: Armor Items, Hotbar Items, Inventory Items, Inventory Menu Items, Potion Effects, Passengers, Attached Leads, Event Command Arguments, Selection Target Names, Selection Target UUIDs.

Repeat: Repeat for Each in List. It's a normal Repeat action, except it repeats once for every index in a list.

If Variable: There are two IfVars that have list parameters: IfVar List Contains Value & IfVar List Value Equals. These are self explanatory.

Part 4: Applications of Lists

Lists have many applications due to how versatile and useful they are. Particular uses include but are not limited to work with Select Object (for more information regarding SelObj, see here), storage of level progress, and repeating code for multiple values.
 

grobulae

Moderator
Moderator
Joined
Sep 6, 2020
Messages
127
Reaction score
26
Good guide but I think a more visual example of a list would be nice
something like:


A list is like an array, which you might recognize. Basically, it's like a grocery list! If you were to want to remember what you're going to buy, you would make a list! For example:

Grocery List (The var name)
Tomatoes
Potatoes
Apples
Flour
Eggs
Milk
Spoons
 
  • Like
Reactions: RDM
Top Bottom