• 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!

Legacy GM (Solved) Is there a way to count the number of sprites in a game?

J

jb skaggs

Guest
How would I go about counting the number of sprites in a game and then sort them into groups?

lets say I have 500 sprites- but I need to find out how many are floor sprites versus weapon sprites?

i'd assume I would name the sprites:

sprite_floor0
sprite_floor1
sprite_weapon0
sprite_weapon1 etc

1: how does one get the total number of sprites?
2: What would be the best way to store and group the list? As a ds_list?

Thanks
JB
 

obscene

Member
Look in your project folder in windows. Also there is a program out there somewhere (google it) called GMPal.exe which can tell you stuff like this as well as give you some nice search and replace features.
 
J

jb skaggs

Guest
Thanks for the response, but I not sure I made my self clear.

The reason I ask, is I want to be able to dynamically create objects based on the number of sprites, and the number will change as the sprites are loaded or deleted/
 
C

CROmartin

Guest
Thanks for the response, but I not sure I made my self clear.

The reason I ask, is I want to be able to dynamically create objects based on the number of sprites, and the number will change as the sprites are loaded or deleted/
I didnt read whole text and I miss understand you so if you read it my frst post ignore it. I would make obj_counter and in it I would put variables sprite_1 , sprite_2. In the objects that have sprite_1 in create I would put obj_counter.sprite_1 += 1 (so every time there is object with sprite_1 obj_counter will know it) and also I would put in on places in code where it changes sprite or die obj_counter.sprite_1 -= 1 . So if you need variable that needs to know how much are sprites of that tipe you just write what_ever_your_var_is_named = obj_counter.sprite_1 . I hope you get it
 
Last edited:

Perseus

Not Medusa
Forum Staff
Moderator
Yes, via asset_get_index and sprite_exists.

Code:
var run = true;
var i = 0;
while (run) {
   var ind = asset_get_index("spr_weapon" + string(i));
   if (sprite_exists(ind)) {
      ds_list_add(weapons, ind);
   }
   else {
      run = false;
   }
   i += 1;
}
As for getting the number of sprites, use ds_list_size.
 

Tsa05

Member
@Ragarnak -- isn't that only going to work as long as sprite index 0 is a weapon?
He'll probably need to grab all sprites, then go through separating out ones named spr_weapon
 

Perseus

Not Medusa
Forum Staff
Moderator
@Ragarnak -- isn't that only going to work as long as sprite index 0 is a weapon?
I don't see why that would happen. Since he's going to name his sprite this way:

Code:
spr_weapon0
spr_weapon1
spr_weapon2
......
spr_weapon99
...I form a string by combining `spr_weapon` with a number (starting at 0), then use asset_get_index to get its index. If such a sprite exists, its index is returned, so sprite_exists returns true and the index is added to the list. If it doesn't exist, -1 is returned which causes sprite_exists to return false, thus setting run to false as well and the while statement ends. If it doesn't end, i is incremented and the process repeats itself. But I might be missing something, so let me know why you think of it that way.

He'll probably need to grab all sprites, then go through separating out ones named spr_weapon
Yes, that's an option too. Possibly the best choice if he decides to use clear names for the sprites, such as `spr_weapon_knife` and `spr_weapon_gun`. He could use sprite_get_name with string_copy for each index, then compare it with `spr_weapon`.
 
J

jb skaggs

Guest
Hey you guys are angels! Thanks for the help! I'll try it and see when I get home from work.
 
Top