Introducing Dragonfly Cloud! Learn More

Question: How can I manage lists in GameMaker code?

Answer

In GameMaker, managing lists is done through the use of specific functions that handle list creation, modification, and destruction. A list in GameMaker is a data structure that can store an ordered collection of values.

Here's an example on how to create a list, add some values, retrieve them, and finally clean up by deleting the list when it's no longer needed:

// Create a new list var my_list; my_list = ds_list_create(); // Add some values to the list ds_list_add(my_list, "Apple"); ds_list_add(my_list, "Banana"); ds_list_add(my_list, "Cherry"); // Retrieve values from the list using accessors or ds_list_find_value() var first_item = my_list[| 0]; // Apple var second_item = ds_list_find_value(my_list, 1); // Banana // You can also change the value at a specific index my_list[| 2] = "Blueberry"; // Loop through the list items for (var i = 0; i < ds_list_size(my_list); i++) { var fruit = my_list[| i]; // Do something with fruit } // Once you're done with the list, make sure to free the memory ds_list_destroy(my_list);

The above code snippet demonstrates basic operations such as creating a list (ds_list_create), adding elements to it (ds_list_add), retrieving elements (ds_list_find_value or by using the accessor [| i] where i is the index), modifying elements directly with the accessor, iterating over the list, and cleaning up memory with ds_list_destroy.

When working with lists in GameMaker, ensure that you manage memory appropriately by destroying any lists you create once you're finished with them to prevent memory leaks.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.