GML [SOLVED] Comparing string variables and getting rid of duplicates

Mr Giff

Member
Hi guys! I've been a long time Gamemaker user (since gm6) but have only recently joined the new forums here. So thank you in advance to anyone who's willing to help me out!

Introductions aside, I've been having an issue that's been bugging me for a week.

First, here's a little pretext on what I am trying to do:
My game has a spreadsheet like system that allows you to move auto populated cell objects around in the room and snap them to a grid. The variables and contents that these objects contain are given to them by a control object upon creation. This control object retrieves their text from a column in a CSV file.

My problem is, is that in the CSV there are more than one of the same cell content. So say there is 50 Apples, 3 Oranges, and 20 Bananas. I want to have three cells say Apples, Oranges and Bananas. However no matter how I try to get rid of duplicate strings I'll still get results that say "Apples, Apples Apples."

I've tried flat out comparison such as:
Code: if global.cellcategory1 == ds_grid_get("CSV",11,nx_cell);

nx_cell as a variable that moves to the next row.

As well as comparing two variables:
Code: if global.cellcategory1 == global.cellcategory2 etc.

I've also tried using string_pos and switch cases but I get same result.
All of my attempts have been in the step event.

If anyone can help me out that would be amazing!

(I've also provided a snapshot of my latest attempt in code)

Cheers.
 

Attachments

Last edited:

jo-thijs

Member
Hi guys! I've been a long time Gamemaker user (since gm6) but have only recently joined the new forums here. So thank you in advance to anyone who's willing to help me out!

Introductions aside, I've been having an issue that's been bugging me for a week.

First, here's a little pretext on what I am trying to do:
My game has a spreadsheet like system that allows you to move auto populated cell objects around in the room and snap them to a grid. The variables and contents that these objects contain are given to them by a control object upon creation. This control object retrieves their text from a column in a CSV file.

My problem is, is that in the CSV there are more than one of the same cell content. So say there is 50 Apples, 3 Oranges, and 20 Bananas. I want to have three cells say Apples, Oranges and Bananas. However no matter how I try to get rid of duplicate strings I'll still get results that say "Apples, Apples Apples."

I've tried flat out comparison such as: if global.cellcategory1 == ds_grid_get("CSV",11,nx_cell);
nx_cell as a variable that moves to the next row.

As well as comparing two variables: if global.cellcategory1 == global.cellcategory2 etc.

I've also tried using string_pos and switch cases but I get same result.
All of my attempts have been in the step event.

If anyone can help me out that would be amazing!

(I've also provided a snapshot of my latest attempt in code)
(I am new... I haven't figured out how to add code in blocks to my post lol).

Cheers.
Hi and welcome to the GMC!

The main error in your code is that you're using strings as ds_grid identifiers.
Data structure identifiers are always numbers though in GameMaker, so it doesn't make sense to use strings.
Where have you used ds_grid_create?
Have you stored its result in a variable?
In that case, you should use that variable as grid id, not an arbitrary string.
 

Mr Giff

Member
Hi and welcome to the GMC!

The main error in your code is that you're using strings as ds_grid identifiers.
Data structure identifiers are always numbers though in GameMaker, so it doesn't make sense to use strings.
Where have you used ds_grid_create?
Have you stored its result in a variable?
In that case, you should use that variable as grid id, not an arbitrary string.
Thanks for the quick reply! ds_grid_create is in the create event of the control object I mentioned earlier which spawns the cell objects. So you're saying I should be referencing the grid id's? When I do use my current method and draw the variables they do show up as text strings not numbers. Unless I'm still confused. What would you recommend is an efficient way for referencing the grid and then storing the contents as a string in a variable?

I should also note that I am using the grid to store the cell contents from the CSV file. I'm then reading the CSV content as strings and I want to compare those strings to make sure there are no duplicates.

Thanks again!
 
Last edited:

jo-thijs

Member
Thanks for the quick reply! ds_grid_create is in the create event of the control object I mentioned earlier which spawns the cell objects. So you're saying I should be referencing the grid id's? When I do use my current method and draw the variables they do show up as text strings not numbers. Unless I'm still confused. What would you recommend is an efficient way for referencing the grid and then storing the contents as a string in a variable?

Thanks again!
That's not completely what I meant.
What you do is this:
Code:
init = ds_grid_create(width, height);
...
value = ds_grid_get("init", column, row);
correct?

Well, that should be:
Code:
init = ds_grid_create(width, height);
...
value = ds_grid_get(init, column, row);
 

Mr Giff

Member
That's not completely what I meant.
What you do is this:
Code:
init = ds_grid_create(width, height);
...
value = ds_grid_get("init", column, row);
correct?

Well, that should be:
Code:
init = ds_grid_create(width, height);
...
value = ds_grid_get(init, column, row);
Oh! I see what you mean! I'll try that immediately and get see if it works.
 

Mr Giff

Member
So my other code appears to be ok. But the same problem persists that I need a method of comparing two strings.
 

Neptune

Member
I'm thinking two grids might be helpful.
One grid that has 50 apples, 3 oranges and 20 bananas -- and another that has apple, orange, banana.

Code:
for(j = 0; j < 100; j++)
{
    for(i = 0; i < 100; i++)
    {
        //Acquires cell value of main grid.
        var fruit = main_grid[# i,j];
       
        //Checks to see if value exists in the secondary grid, and adds it otherwise.
        if !ds_grid_value_exists(secondary_grid,0,0,ds_grid_width(secondary_grid)-1,0,fruit)
        {
            secondary_grid[# ds_grid_width(secondary_grid)-1,0] = fruit;
            ds_grid_resize(secondary_grid,ds_grid_width(secondary_grid)+1,1);
        }
    }
}
If you try this method, just initialize 'secondary_grid' as width 1 and height 1. This code should scan through the main grid, and it will check each value (real or string) to see if it is in the secondary grid -- leaving you with a grid that has only each fruit type. (Don't forget to adjust the for-loops to fit the size of the grid you are scanning.)
 

Mr Giff

Member
I'm thinking two grids might be helpful.
One grid that has 50 apples, 3 oranges and 20 bananas -- and another that has apple, orange, banana.

Code:
for(j = 0; j < 100; j++)
{
    for(i = 0; i < 100; i++)
    {
        //Acquires cell value of main grid.
        var fruit = main_grid[# i,j];
      
        //Checks to see if value exists in the secondary grid, and adds it otherwise.
        if !ds_grid_value_exists(secondary_grid,0,0,ds_grid_width(secondary_grid)-1,0,fruit)
        {
            secondary_grid[# ds_grid_width(secondary_grid)-1,0] = fruit;
            ds_grid_resize(secondary_grid,ds_grid_width(secondary_grid)+1,1);
        }
    }
}
If you try this method, just initialize 'secondary_grid' as width 1 and height 1. This code should scan through the main grid, and it will check each value (real or string) to see if it is in the secondary grid -- leaving you with a grid that has only each fruit type. (Don't forget to adjust the for-loops to fit the size of the grid you are scanning.)
Thank you very much! This would normally work, however the CSV is written differently for each game based on what the user inputs. So I couldn't make a second grid to check because I don't know what those inputs will be. I just need to check if one header cell has different information / input text than another cell. All of this occurs when a previous "spreadsheet" has been loaded.
 

jo-thijs

Member
Thank you very much! This would normally work, however the CSV is written differently for each game based on what the user inputs. So I couldn't make a second grid to check because I don't know what those inputs will be. I just need to check if one header cell has different information / input text than another cell. All of this occurs when a previous "spreadsheet" has been loaded.
So, your issue not yet solved, right?

I'm not sure if I understand you completely.

You want to check if there are 2 distinct cells in the first row of a ds_grid (which is filled with strings) contain the same value, right?
Then, what is the meaning of all the variables in your code?
And if cells with the same value are found, what do you want to do with them?
 

Mr Giff

Member
Ah, sorry. No it is not.

There is a DS grid which is created via a CSV file. The eleventh column in that grid contains a series of names, many of which will be duplicates. The table in the game has header cell objects at the top of the table and everything below are normal cell objects. The normal cell objects auto populate under whichever header their content corresponds to on the grid. Going back to the fruit example, Apple slices would go under the category of Apple and etc.

The problem is the header elements are not assigning themselves distinct titles resulting in duplicate columns. I want the headers to check each other to make sure they don't have a string that is already being used. If the category checks it's own string against one from another category (catbox1 vs catbox2) then it needs to scan the column of the grid again until it finds a unique string. If it cannot find a unique string then return Undefined.

Thanks for being patient with me! I underestimated how hard this is to describe.

Essentially. All I need is a way of checking to see if two variables hold the exact same string, and I don't know what that string is because it changes.
 

Neptune

Member
So, to check if two variables hold the exact same string:
Code:
if (str1 == str2) {/*they are exactly the same!*/}
 

Mr Giff

Member
So, to check if two variables hold the exact same string:
Code:
if (str1 == str2) {/*they are exactly the same!*/}
Hmmm, I tried that and something similar before but once there is more than two cells to check things don't work correctly or just return undefined for some reason.

Fore example this is what is in the step event of my third cell:

Code:
if ds_exists ("init",ds_type_grid)
{
if global.newload == true
{
if (global.catbox3 == global.catbox2) {global.catbox3 = ds_grid_get(init,11,nx_cell+1);}
if (global.catbox3 == global.catbox1) {global.catbox3 = ds_grid_get(init,11,nx_cell+1);}
}
}
The goal is that if it is the exact same as something else... Search for a new string in the grid by one each step.
 

Mr Giff

Member
Hey Guys! I figured it out!

So my problem was that I was comparing the variables and changing the variables with every step! So they could never not find a match and would just infinitely loop.

Thank you guys so much you helped me try a bunch more options and rethink and simplify my code, you're the real heroes. :)
 

Mr Giff

Member
Also thanks Jo-thijs for clarifying the grid identifier problem. That was messing things up elsewhere.
 
Top