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:
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:
Let's understand what is written:
2.2. Add a new event: Key Pressed >> Space:
Now, add this:
Let's understand what is written:
2.3. Draw the list:
Add a new event: Draw >> Draw GUI
Let's understand what is written:
#3. Result:
#4. Bonus:
Extended tutorial, searching in an inventory.
In the future.
Hope you like it.
Thanks!
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";
}
Code Bank - share useful code!
Quicksort for sorting by some property of elements of arrays function shuffle = function(array) { for (var i = array_length(array) - 1; i >= 0; i++) { array[@ i] = array[irandom(i)]; } } function array_sort_by(array, map) { static _swap = function(array, a, b) { var c = array[a]...
forum.yoyogames.com
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;
- 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;
}
- First I check if the "
searching
" variable is equal tofalse
, 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;
}
- 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 usedstring_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: