• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Help - Search Bar for Data Structures.

Mr Giff

Member
Hey guys, I'm not sure how to go about this. I have a game with ds grid used for inventory. I want to be able to search that inventory with a search bar.

I currently have a search bar object which draws input text in real time to the sprite's width and height. That text is assigned to a global variable on every step so that it can be used to dictate what is being shown.

I can't find any way of searching a data structure however. Besides queries, where the ID or position in the grid is required.

I found a way of getting it to return exact names, but only if you type the whole name. For example, if you were to type "sword"... Nothing would show. But if you typed broad sword or short sword, a broad sword or short sword would be the only result respectively.

What I am looking for is to be able to type "S" or "Sword" and have all the results that contain "S" or "Sword" to be shown instantaneously

What are your thoughts? Is there a way to make a search engine / contextual search in game maker?
Any DLL's that might do the trick?

Cheers!
 
You don't need a dll.

Use string_count(search_term, name). If the string_count is greater than 1, than it's a hit. You also may want to use string_upper or string_lower to make your search case insensitive.
 

Mr Giff

Member
I thought either string_count or string_pos, but wasn't sure.. Alright. So say lets see if this would work?

Code:
// In Create Event
term=0;
row=1; //The starting row in the grid column holding item names.
column=1; //Likewise^
hits=0;
srl=0; //Container for search results string in lowercase.

// In Step Event
if global.search_results != ""; 
{
term = ds_grid_get(items,column,row); 
srl = string_lower(global.search_results);
hits = string_count(srl,term); 

// Then I'm not sure what happens next, how do I return results from the grid using this value?
 
@Mr Giff
So, here's what i would do.

You need two data structures: your grid, which is a static "lookup table" and then your results, which would be a ds_list of row numbers for results.

As you search through the rows of your grid and find results, add the current row to the ds_list.

Then once the search is complete, display the results by looping through the list, and grabbing the relevant data off the row from the original grid.
 

Mr Giff

Member
@Pixelated_Pope So I've been working on it all weekend. And rather than go with a DS_List I decided to go with a seccond DS_Grid for more functionality in the future. My current code is intended to return the top ten results closest to the given string if not exactly the given string. Only problem is, nothing shows up at all.

Here it is below, SE is in front of all if not most of the variables because those same variable names are used for another grid in the object. Once I get the concept working I'll probably turn this into an array to simplify it:

Code:
/// "Similarity = Rank" Search Engine

//Initialize if there is a query
if global.search_results !="" ^^ undefined
    {
    SEterm = ds_grid_get("items",SEcolumn,SErow);
    SEsrl = string_lower(global.search_results);
    SEhits = string_count(SEsrl,string(SEterm));
    }
//Scan rows and return 10 prioritized results to ds_grid ""results""
if SEhits == SEsrhits 
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
if SEhits == SEsrhits -1
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
if SEhits == SEsrhits -2 
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
if SEhits == SEsrhits -3
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
if SEhits == SEsrhits -4
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
if SEhits == SEsrhits -5
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
if SEhits == SEsrhits -6
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
if SEhits == SEsrhits -7
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
if SEhits == SEsrhits -8
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
if SEhits == SEsrhits -9
    {ds_grid_add("results",1,SErow,SEsrl); SErow+=1}
 
That may be so, but you are using ds_grid functions. and they do not work that way. So unless what you posted above is pseudo code, that's your problem.

GM Doesn't yell at you when you use a string in a ds_grid as the index (even though I submitted a bug, yoyo has said this is intended; god knows why). As far as I can tell, any string passed as the ID in a ds_grid call will be interpreted as 0, which is always the first ds_grid you ever created.
 

Mr Giff

Member
Oh! You're right! When I was drawing the variable "SEterm" it was always returning as 0 and I couldn't figure out why. I'll redefine the variable and get back, see if it fixed the problem.
 
Top