inventory | gml
| Type | Desc | |
|---|---|---|
| ds_list | data structure (list) | like an array but with some cool built-in functions, like ds_list_create(), ds_list_add(), ds_list_empty(), ds_list_size() and ds_list_find_value() |
| ds_list_find_value(name_of_ds_list, index) ds_list_delete(name_of_ds_list, index) | functions | retrieves or deletes the value at the specific index |
| ds_list_size(name_of_ds_list) | function | returns size of ds_list |
| ds_list_add(name_of_ds_list, value) | function | adds an item to a ds_list |
| instance_number(name_of_object) | function | returns the instance count, i.e. the number of current spawns of an object in the room |
| Sprite | Images |
|---|---|
| sprHuman | ![]() |
| sprCoin | ![]() |
| sprKey | ![]() |
| Object | Sprite | Properties |
|---|---|---|
| objHuman | sprHuman | Visible |
| objCoin | sprCoin | Visible |
| objKey | sprKey | Visible |
Leave the key out of room for now, we will spawn the key in once all coins have been collected.
objHuman Create
//ds = datastructure - such as stacks, queues, lists, maps, grids etc. inventory = ds_list_create(); //flag so that we only spawn one key when all coins collected: spawned_key_yet = false;
objHuman Step
//------------movement:
switch (keyboard_key) {
case vk_left: x-=5; break;
case vk_right: x+=5; break;
case vk_up: y-=5; break;
case vk_down: y+=5; break;
}
//------------collectibles:
coin = instance_place(x,y,objCoin);
if (coin != noone) {
ds_list_add(inventory, "Coin");
instance_destroy(coin);
}
key = instance_place(x,y,objKey);
if (key != noone) {
ds_list_add(inventory, "Key");
instance_destroy(key);
}
//------------drop a collected coin:
//if there are items in the inventory:
if !ds_list_empty(inventory)
{
if (keyboard_key == vk_space){
//get size of inventory so we can parse every item looking for a coin:
size = ds_list_size(inventory);
//for each item in the inventory:
for (i = 0; i < size; i=i+1){
//retrieve the name of the item:
item = ds_list_find_value(inventory, i);
if (item == "Coin"){
//remove the coin:
ds_list_delete(inventory, i);
//put it back on the map somewhere random:
randomize();
instance_create_layer(random(room_width),random(room_height),"Instances",objCoin);
//break out of the for loop once a coin has been dropped:
break;
}
}
}
}
//------------make key appear once coins collected:
if (instance_number(objCoin) < 1 and !spawned_key_yet){
instance_create_layer(300,300,"Instances",objKey);
spawned_key_yet = true;
}
objHuman Draw GUI
//Draw the items being carried by the player:
if !ds_list_empty(inventory)
{
//get size of inventory so we can parse every item:
size = ds_list_size(inventory);
//for each item in the inventory:
for (i = 0; i < size; i=i+1){
//retrieve the name of the item:
item = ds_list_find_value(inventory, i);
//i * 20 is the y-axis, so draw items down the side:
draw_text(5, i * 20, item);
}
}
Extension Activities
Turn this into a game



