Data Structures (DS) are similar in nature to arrays, but you can do so much more with them!
Sort names (or other strings) alphabetically, numbers, and more – descending or ascending
Store a player inventory
Shuffle lists randomly
Manage and queue up audio or graphical effects
Message management
Store and process lists of numbers
Remove an item, and the list will reorganize itself
Works with variables, ids, sprites, objects, etc.
Process commands in order
Insert contents anywhere on the list
Search a list for an element
Creating a DS List
This tells GameMaker that this variable example_list is a ds list.
This adds these strings to the ds list, in the preceding order.
You can add multiple elements in one line of code, for example: ds_list_add(example_list,"Earth","Mars","Jupiter,"Venus", "Pluto");
[yes I know Pluto is no longer a planet]
Index | Value |
---|---|
0 | “Earth” |
1 | “Mars” |
2 | “Jupiter” |
3 | “Venus” |
4 | “Pluto” |
Like other data structures in GameMaker, the first index is 0, so a list with 5 values would have the locations of 0 through 4.
Sorting an Array
This sorts elements in the list alphabetically.
Using false would organize it in reverse order.
Index | Value |
---|---|
0 | “Earth” |
1 | “Jupiter” |
2 | “Mars” |
3 | “Pluto” |
4 | “Venus” |
Removing a Value
would remove the value of index 3 (namely “Pluto”).
Index | Value |
---|---|
0 | “Earth” |
1 | “Jupiter” |
2 | “Mars” |
3 | “Venus” |
Index value
Adding a Value
inserts new data at position 1.
Index | Value |
---|---|
0 | “Earth” |
1 | “Neptune” |
2 | “Jupiter” |
3 | “Mars” |
4 | “Venus” |
After adding an element you may want to sort your list again.
which would set the value of position as 3.
sets the variable to what is held at position 4.
sets the variable to the list size, see the following note.
As a list starts it’s reference 0, a list with four elements would return as four (there are contents at positions 0, 1, 2, and 3.
randomly shuffles the list.
removes the given ds list from memory.
Basic Projects
- A)
Make a system that can hold five variables. If player presses X, allow them to add a new variable at the bottom of the list, and remove the top item. Draw this data onscreen.
- B)
Populate a list with five fruits. Player to enter a guess of a fruit. If this is in the list, remove it and tell the player. When all have been guessed, the player wins.
- C)
Make a system that draws the names of all students in your class in alphabetical order.
Advanced Project
- D)
Populate a list with playing cards (use a loop for this), that is, AS for ace of spades, 5D for the five of diamonds. Shuffle this list, and deal out top five cards (and remove from the list) into 4 players’ hands. Draw the players’ hands onscreen.
Useful Functions
allows for saving and loading data
Summary
You’re now able to create a data structure, add, remove, and sort elements. You should now be able to identify when a ds list should be an appropriate data structure to use.