GML Search Items In An Array (for inventory)

FoxyOfJungle

Kazan Games
GM Version: GMS 2+
Target Platform: ALL
Download: N/A
Links: N/A
Language: GML


Summary:

Search for items in an array. I write a string, and it will find all items matching the search.


Tutorial:

Do you want to make an inventory or a list of some store/shop or a highscore list and want to show only the items corresponding to the search? This tutorial is for you.


#1. The first thing we need to know is: What are arrays?

An array is simply a data type that is assigned to a variable, and it can contain not just one value, but multiple values.
Arrays can be initialized in different ways:

GML:
// Type: 1
// Create an array and associate it with the "array" variable.
// Then, say directly what each item corresponds to.
array[0] = "Fox";
array[1] = "Of";
array[2] = "Jungle";


// Type: 2
// This way, you will associate an array with the "array" variable, but in a more compact way.
// Note that it is still 0 to 2, keep that in mind.
//         0      1     2
array = ["Fox", "Of", "Jungle"];


// Type: 3
// Create an array with lenght of 3 where everyone is equal to 0.
array = array_create(3);


// Type: 4
// Create an array where everyone is equal to the string "cherry".
// Note that this method is a little bit heavier.
array = array_create(3, "cherry");


// Type: 5
// Create an array where everyone is equal to the string "cherry".
// You don't necessarily need to use this method, just in case you do.
for (var i = 0; i < 3; ++i)
{
    array[i] = "cherry";
}
An example of use:

If you want to know how array works, take a look at the manual.


An array can also have multiple dimensions, but in this tutorial we will cover only a simple array of just 1 demension. However, this tutorial can be expanded to more.




#2. Let's get started

2.1. Create a new empty object, and add it to the Create Event:

GML:
array[0] = "pickles";
array[1] = "apple";
array[2] = "apple cherry";
array[3] = "cherry_Apple";
array[4] = "coConut";
array[5] = "Orange";

search_str = "";
searching = false;
Let's understand what is written:
  • I created an array and associated it with the "array" variable .
  • I added several strings, they will be what we will search for. Note that there are strings with similar names, this is purposeful.
  • I created the variable "search_str", it will indicate what to search for.
  • I created the variable "searching", it will indicate if we are searching or not.


2.2. Add a new event: Key Pressed >> Space:
Now, add this:

GML:
if !(searching)
{
    search_str = get_string("search string", "appl");
    searching = true;
}
else
{
    searching = false;
}
Let's understand what is written:
  • First I check if the "searching" variable is equal to false, that is, if we are not searching.
  • If it is true, then, I set our "search_str" variable to a new string that we will search for.
  • After the else, we will just reset so that we can view the normal list, before searching, if I press Space again.


2.3. Draw the list:
Add a new event: Draw >> Draw GUI

GML:
var _xx = 10
var _yy = 10;
for (var i = 0; i < array_length(array); ++i)
{
    if (searching)
    {
        if !string_pos(string_lower(search_str), string_lower(array[i])) continue;
    }

    draw_text(_xx, _yy, array[i]);

    _yy += 30;
}
Let's understand what is written:
  • I will draw the text in the x, y position: (10, 10).
  • I set the starting position of the x and y to 10, 10.
  • I'm looping through all the items on the list.
  • Where is (searching), I will search only if the variable is true.
  • Through string_pos, I will check for each item in our array, so I will draw only the items that match our search (the "search_str" variable). So I use continue so that it "skips" the loop. I used string_lower() because this will prevent problems with uppercase and lowercase characters.
  • Therefore, it will draw only what matches the research, using draw_text().
  • In the end, we increment the item list _yy (y).


#3. Result:





#4. Bonus:
Extended tutorial, searching in an inventory.

In the future.



Hope you like it. 🙂
Thanks!
 
Last edited:

FrostyCat

Member
Your coverage of array setup is very poorly researched and completely not fact-checked. I really wish people would stop teaching GML in old versions they haven't used or are no longer familiar with.

Error 1:
GML:
// Type: 2
// This way, you will associate an array with the "array" variable, but in a more compact way.
// Note that it is still 0 to 2, keep that in mind.
//         0      1     2
array = ["Fox", "Of", "Jungle"]; // GMS 1.4
array = ["Fox"]["Of"]["Jungle"]; // GMS 2.3+
The array literal syntax was NOT available on GMS 1.4, and what you described as GMS 1.4 syntax is actually GMS 2 syntax. On top of that, what you described as GMS 2.3+ syntax actually doesn't even exist.

Error 2:
An array can also have multiple dimensions:

GML:
// Type: (2-dimensional array)
// GMS 1.4
array[0, 0] = "foxy"; //way 1
array[0, 1] = [value, 10]; //way 2

// GMS 2.3+
array[0][0] = "foxy"; //way 1
array[0][1] = [value, 10]; //way 2


// Type: (3-dimensional array)
// GMS 1.4
array[0, 0, 0] = "foxy"; //way 1
array[0, 1, 1] = [value, 10]; //way 2

// GMS 2.3+
array[0][0][0] = "foxy"; //way 1
array[0][1][1] = [value, 10]; //way 2
Once again, the array literal syntax was not available in GMS 1.4, it was new to GMS 2. On top of that, there were no built-in 3D arrays in GMS 1.4 (so your example would cause a syntax error), and all your "way 2" examples are trying to set up arrays that are one dimension deeper than stated.

Especially with multi-dimensional arrays, the main procedure of handling them has shifted so significantly in GMS 2.3 (e.g. added support for chaining, removal of 2D arrays as a distinct syntax), trying to cover it all in the same tutorial only breeds confusion. It would have been better had you dropped GMS 1.4 altogether, and covered only the latest technique available in GMS 2.3+ if that's what you've been using.
 

FoxyOfJungle

Kazan Games
@FrostyCat
The @chance pointed me to the changes, I ended up forgetting to remove that would be supported in GMS 1.4.

what you described as GMS 2.3+ syntax actually doesn't even exist.
How does it not exist? The syntax has changed.

The focus of the topic is to do research on arrays, not to teach how arrays work. There was only a brief explanation of this. But it can be corrected. Furthermore, there are other words that can be used other than those.
Thanks!
 
Last edited:

FrostyCat

Member
How does it not exist? The syntax has changed.
The syntax for accessing a 2D-or-higher array has changed in GMS 2.3. The syntax for array literals has not.

This is valid GMS 2.3+ syntax:
GML:
array = ["Fox", "Of", "Jungle"];
This is NOT (shows "malformed variable reference got [" in the IDE as of 2.3.1.542):
GML:
array = ["Fox"]["Of"]["Jungle"];
 
Top