A question about ds_lists

G

GreatJustice

Guest
So how do I get my game to count the number of the same values in a ds_list?

For example if I had a list called "fish", I want to know the number of "red fish" that are in the list, but not the number of "blue fish" or 'green fish".
 
You would loop through the list and count the values.

Code:
var _red_count=0;
for(var _i=0; _i<ds_list_size(fish);_i++)
{
   _red_count += fish[| _i]=="Red Fish";
}
 
B

Blazing

Guest
Use a repeat loop the size of the list to check each index in the list. If it comes across a red fish, add one to a variable. Then you can use that variable after the loop for whatever purpose you need.
 
If it were me, I'd use a DS Map, roughly like this:

Code:
var map = ds_map_create(),
    c1 = 0;

repeat (ds_list_size(list))
{
    if (is_undefined(map[? list[| c1]))
    {
        map[? list[| c1] = 1; // First entry
    }
    else
    {
        map[? list[| c1]]++; // Increase the count
    }

    c1++;
}

// ...  Then you can iterate through the map and find out how many of each entries there are.
var key = dS_map_find_first(map);

repeat (ds_map_size(map))
{
    show_message(key + ": " + string(map[? key])); // Shows the value and how many times it was found in the list
    key = ds_map_find_next(map, key);
}
... Might not be the most efficient method, but it would work well for a larger list.

Edit: Should have mentioned that the code I gave would tally up the number of all different items in the list. If you just needed to find the number of a single value, then a loop, if, and incrementing counter will do fine.
 
Last edited:
G

GreatJustice

Guest
You would loop through the list and count the values.

Code:
var _red_count=0;
for(var _i=0; _i<ds_list_size(fish);_i++)
{
   _red_count += fish[| _i]=="Red Fish";
}
I've been trying to apply this to the game I'm working on and it didn't quite work.

For context I'm making a top-down shooter where the number of weapon items of the same type determine the number bullets you shoot at one time. For example if you have three lasers items you'll shoot out three laser bullets.

Here's the code I used for the Create Event
Code:
//Duplicate Check

laser_amount = 0
fire_amount = 0
thunder_amount = 0
and here's what I used in the Step Event
Code:
//Duplicate Check
for(var _i1=0; _i1<ds_list_size(inventory);_i1++)
{
   laser_amount = inventory[| _i1]=="laser";
}


for(var _i2=0; _i2<ds_list_size(inventory);_i2++)
{
   fire_amount = inventory[| _i2]=="fire";
}

for(var _i3=0; _i3<ds_list_size(inventory);_i3++)
{
   thunder_amount = inventory[| _i3]=="thunder";
}
 

FrostyCat

Redemption Seeker
Quite clearly you haven't traced out the code Pixelated_Pope gave you, because if you did the problem would be quite obvious.

Compare his main loop:
Code:
for(var _i=0; _i<ds_list_size(fish);_i++)
{
   _red_count += fish[| _i]=="Red Fish";
}
With yours: (all of them have the same problem)
Code:
for(var _i1=0; _i1<ds_list_size(inventory);_i1++)
{
   laser_amount = inventory[| _i1]=="laser";
}
Pixelated_Pope is incrementing by the equivalence (+=), you are setting it directly to the equivalence (=).
 
G

GreatJustice

Guest
Quite clearly you haven't traced out the code Pixelated_Pope gave you, because if you did the problem would be quite obvious.

Compare his main loop:
Code:
for(var _i=0; _i<ds_list_size(fish);_i++)
{
   _red_count += fish[| _i]=="Red Fish";
}
With yours: (all of them have the same problem)
Code:
for(var _i1=0; _i1<ds_list_size(inventory);_i1++)
{
   laser_amount = inventory[| _i1]=="laser";
}
Pixelated_Pope is incrementing by the equivalence (+=), you are setting it directly to the equivalence (=).
I've done that before and laser_amount continued to just count forever.
 
Top