GML Looking Through Two Arrays for Duplicates?

Divinik

Member
So I have a ds_grid that holds all items in the game. In the Inventory's Menu, there are tabs that copy certain "Types" of items from the master grid and creates it's own ds_grid for that tab.

I'm trying to figure out how to delete the same item from from the sub arrays and the main array when a player wants to delete and item.

Here's some pictures:

upload_2019-5-6_15-15-59.png

upload_2019-5-6_15-16-32.png

Anyone know how I can achieve this? I'm a bit of a ds_grid noob haha.

P.S. All weapons (even same type) have different stats from each other, so they are able to be differentiated on the grid.
 
T

Timothy

Guest
Well you need some sort of unique identifier... but since I know nothing about your items, I can't say what that is... but why are you using multiple arrays anyway? Just filter straight from the ds_grid and eliminate your issue.

Edit one thing you could do is use a ds_grid as a database of sorts and it's the only place the real items are stored. Then all other tables just pull information from the grid via it's position as an id.
 

Divinik

Member
but why are you using multiple arrays anyway? Just filter straight from the ds_grid and eliminate your issue.
Would you be willing to show me how to display only a certain "type" of item?

The grid is set up as follows

  1. ItemAmount
  2. ItemDescription
  3. ItemSprite
  4. ItemScript
  5. ItemType
  6. ItemIcon
  7. ItemPower
  8. ItemHP
  9. ItemAccuracy
 

samspade

Member
So I have a ds_grid that holds all items in the game. In the Inventory's Menu, there are tabs that copy certain "Types" of items from the master grid and creates it's own ds_grid for that tab.

I'm trying to figure out how to delete the same item from from the sub arrays and the main array when a player wants to delete and item.

Here's some pictures:

View attachment 24661

View attachment 24662

Anyone know how I can achieve this? I'm a bit of a ds_grid noob haha.

P.S. All weapons (even same type) have different stats from each other, so they are able to be differentiated on the grid.
You have to manually loop and search the grid. This is one of the reasons I stopped using grids entirely and instead use lists or maps if pure arrays won't work.

Looking for a value in an array, list, or grid simply means iterating through all the possibilities and checking (except that lists have a built in function for this so you don't need to write you own).

Code:
for (var i = 0; i < size; i += 1) {
    if (value == thing[i]) {
        you found it
    }
}
]

Substitute the correct variables and accessors depending on the structure.

The above method will only work assuming you have a way of identifying the thing in the grid. If all items of the same type are identical, then you can simply search for the first match. Otherwise, you'll need another variable such as ItemName which would most likely be a string or unique id so that you can figure out which thing you're looking for.

In general, grids are the least useful (in my opinion) of arrays, grids, lists, and maps. I would suggest never using them unless you want access to one of the ds_grid specific functions and you don't know how to code the same function yourself (or the costs of doing so outweigh the benefits). Beyond a few unique (and situationally very helpful) additional functions, and the ability to access by row and column at the same time, they are a massive headache to use since they don't do things like resize automatically and have significantly less functionality than their corresponding ds_list. For example, you can't do an automatic search like you want amd you can't easily save them in the json format.
 
T

Timothy

Guest
You have to manually loop and search the grid. This is one of the reasons I stopped using grids entirely and instead use lists or maps if pure arrays won't work.

Looking for a value in an array, list, or grid simply means iterating through all the possibilities and checking (except that lists have a built in function for this so you don't need to write you own).

Code:
for (var i = 0; i < size; i += 1) {
    if (value == thing[i]) {
        you found it
    }
}
]

Substitute the correct variables and accessors depending on the structure.

The above method will only work assuming you have a way of identifying the thing in the grid. If all items of the same type are identical, then you can simply search for the first match. Otherwise, you'll need another variable such as ItemName which would most likely be a string or unique id so that you can figure out which thing you're looking for.

In general, grids are the least useful (in my opinion) of arrays, grids, lists, and maps. I would suggest never using them unless you want access to one of the ds_grid specific functions and you don't know how to code the same function yourself (or the costs of doing so outweigh the benefits). Beyond a few unique (and situationally very helpful) additional functions, and the ability to access by row and column at the same time, they are a massive headache to use since they don't do things like resize automatically and have significantly less functionality than their corresponding ds_list. For example, you can't do an automatic search like you want amd you can't easily save them in the json format.
I'd mostly agree with this, but grids can make good inventories if you plan on having a set number of max items in your inventory.

Would you be willing to show me how to display only a certain "type" of item?

The grid is set up as follows

  1. ItemAmount
  2. ItemDescription
  3. ItemSprite
  4. ItemScript
  5. ItemType
  6. ItemIcon
  7. ItemPower
  8. ItemHP
  9. ItemAccuracy
Its pretty easy to sort by ItemType... its built in to the grid structure. Then just draw from there. I mean I can whip up an example code if you need... But personally i would suggest modeling your items as an array since i really doubt all your items have every single one of those properties. You can still structure your inventory with a grid and have it contain the ItemType as a column. Something like
slot, type, item
0, gun , [properties...]

Its not necessary, just a suggestion.
 

Divinik

Member
I ended up figuring it out, If anyone runs into the same issue here's how I did it:

In the STEP EVENT:

Code:
   for(k = 0; k < ds_grid_height(myItems); ++k)
    {
     itemID = ds_grid_get(currentGrid,10,global.itemSelected)
     
     if (myItems[# 10,k] == itemID)
            {
             ghostID = k;
            }
    }
Then just delete the column with the ghostID
 
Top