Introduction
This lesson will walk you through how to create and use the most common list scripts. By the time your done, you’ll be thinking about ways that you can use lists in your game projects.
Part A – Basic List Scripts
In this part you will be introduced the creation of lists and the common pre-made scripts that you can use with lists.
Watch the video: 11-01-A
Notes on lists: GameMaker_Lists.doc (cheat sheet seen in video)
Project File: L11-01-ADone.gmk
Challenge 1 – Inventory List
Start File: L11-01-X1Start.gmk
This challenge involves you setting up an inventory list for the player that keeps track of which food items that have been collected. As the player picks up food, the name i
a) You’ll see that in the challenge room there are apples and burgers. When the player touches an apple the apple should be destroyed and you should add the word “apple” to a list (you can create this list in player->create). Do something similar for when the player contacts burgers.
b) Lets make it so that the player can only carry 0-5 items. Modify your code so that before collecting apples or burgers you check the size of the inventory list to make sure that they are carrying less than 5 items. If the player is already carrying 5 items then don’t destroy the apple or burger and don’t add it to the list.
c) Lets give the player the choice to eat an apple or burger by pressing the “A” or “B” key. If the player presses “A”, find out in what position the word “apple” is in the list using the ds_list_find_index script. If this script returns -1 you know that no apple was found in the list and you should show a message saying “you have no apples!”. If there is an apple in the list, show a message like “just ate apple!” and remove that apple from the list (you just found out where it is, so you should be able to remove it easily with the ds_list_delete script. Test this out to make sure that you can only eat what you have collected!
d) In the draw event of a draw object, find out the size of the list and draw it to the screen. As you collect items you should see the number go up and as you eat items you should see the value go down.
Challenge 1 Solution
Solution Video: 11-01-X1Solution
Solution File: L11-01-X1Done.gmk
Challenge 2 – Magical Touch
In this challenge you will create a list that will store the ‘id’ values of apples that the player touches. When the player contacts an apple, the apple’s id will be added to a list if it isn’t already in the list. When the “D” key is pressed, all the apples that have been touched will be destroyed. I like this challenge because it shows you that you can remember objects by storing their id’s in lists.
Preview Video: L11-01-X2-Preview
Start with: L11-01-X2Start.gmk
a) create a global list that will store the id’s of objects (just like any other list creation)
b) every time a player touches an apple, get the apples id and check to see if it already in the list or not (remember that when searching for a value in a list you use ds_list_find_index and if it returns zero or larger you know the index at which it was found, and -1 means that you didn’t find the value). If the apple id isn’t in the list, add the apple’s id into the list.
c) create a script called destroyFirstApple. This script should find out how many values are stored in the list. If there are no apples in the list, just exit. If there are apples in the list, find the first id in the list (remember that this is in index position 0, use ds_list_find_value). Once you know this id, use a with statement with this id to destroy this apple. Also delete this value from the list (delete the value in position 0). To test this, complete step d.
d) find the o_tester object. When it detects the “D” key pressed, run the destroyFirstApple script. When testing you should see that after you have touched a bunch of apples, the “D” key should delete the apples one at a time in the order you added them to the list!
e) go back to the destroyFirstApple script and add the following: after deleting the apple and it’s id from the list, check to see if the size of the list is larger than zero. If it is, then we have more apples to destroy and you should call the script destroyFirstApple again. Your code will end up looking something like this:
if ds_list_size(global.apples) > 0 {
destroyFirstApple()
}
Now when you test this out, the script will (if there are still apples left) call itself to run again, and again, and again until there are no more apples left in the room. This is probably your first exposure to an idea called recursion. Recursion is when a script calls itself and a type of loop is created. It works!
Challenge 2 Solution
Solution Video: L11-01-X2-Solution
Solution File: L11-01-X2Done.gmk