© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_22

22. DS Lists

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 

Data Structures (DS) are similar in nature to arrays, but you can do so much more with them!

You can do things such as:
  • 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

It is relatively easy to create a ds_list:
example_list = ds_list_create();

This tells GameMaker that this variable example_list is a ds list.

Then can it add some contents
ds_list_add(example_list,"Earth");
ds_list_add(example_list,"Mars");
ds_list_add(example_list,"Jupiter");
ds_list_add(example_list,"Venus");
ds_list_add(example_list,"Pluto");

This adds these strings to the ds list, in the preceding order.

Note

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]

You could visualize this as

Index

Value

0

“Earth”

1

“Mars”

2

“Jupiter”

3

“Venus”

4

“Pluto”

Note

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

You can sort the list:
ds_list_sort(example_list,true);

This sorts elements in the list alphabetically.

Using false would organize it in reverse order.

Organized in order would then look like:

Index

Value

0

“Earth”

1

“Jupiter”

2

“Mars”

3

“Pluto”

4

“Venus”

Removing a Value

You can remove a value, for example, get rid of "Pluto" because it is not a planet:
ds_list_delete(example_list,3);

would remove the value of index 3 (namely “Pluto”).

The ds list would then respond by moving other values up, so the list will then look like:

Index

Value

0

“Earth”

1

“Jupiter”

2

“Mars”

3

“Venus”

Index value

Adding a Value

A new element can be inserted:
ds_list_insert(example_list,1,"Neptune");

inserts new data at position 1.

Elements that follow will be moved down, so the list would then look like this:

Index

Value

0

“Earth”

1

“Neptune”

2

“Jupiter”

3

“Mars”

4

“Venus”

After adding an element you may want to sort your list again.

You can find where (if present) an element value is positioned:
position=ds_list_find_index(example_list,"Mars");

which would set the value of position as 3.

You can get the value of an element at a given location:
chosen=ds_list_find_value(example_list,4);

sets the variable to what is held at position 4.

A very useful bit of code allows you to get a value using the | accessor, so instead of the preceding code, you can obtain the value held directly. For example, an alternative to the preceding line of code could simply be written as:
chosen=example_list[| 4];
You may need to find the size of a list, for example:
list_size=ds_list_size(example_list);

sets the variable to the list size, see the following note.

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.

A list can be shuffled into a random order (great for card games):
ds_list_shuffle(example_list);

randomly shuffles the list.

It is important to get rid of the list when you have finished with it, this helps prevent memory issues:
ds_list_destroy(example_list);

removes the given ds list from memory.

Basic Projects

  1. 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.

     
  2. 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.

     
  3. C)

    Make a system that draws the names of all students in your class in alphabetical order.

     

Advanced Project

  1. 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

ds_list_clear()
ds_list_read()
ds_list_write()

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.

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

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