• 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] How to count number of sprites an object in the room?

B

Bhreno

Guest
I'm trying to do this: with the variables, I need to check the amount of obj_ball in the room and each variable must count the amount of a specific obj_ball sprite.
The perfect code for my problem would be a ... "sprite_number", with the same function as "instance_number", but "sprite_number" does not exist and it is necessary to find another way to count the number of sprites in the room.

(following example of the code I'm looking for).
Code:
with (obj_ball)
default_ball = sprite_number(spr_default_ball); // this should give me an amount of "default_ball" sprite obj_ball that exists around the room. The same applies as other variables. And this check should happen all the time.
yellow_ball = sprite_number(spr_yellow_ball);
blue_ball = sprite_number(spr_blue_ball);
How can I do this check?
 

marasovec

Member
This script should do it
It loops through all instances and checks if the object's sprite is the one you want to check. If it is it adds 1. Then returns total
Code:
/// sprite_number(spr)
var num = 0;
for(var i = 0; i < instance_number(all); i++)
    {
    var inst = instance_find(all, i);
    if inst.sprite_index == argument0 num++;
    }
return num;
 
Last edited:

CloseRange

Member
to improve what @marasovec suggested there are better ways to do it.
Code:
/// sprite_number(spr);
var num = 0;
with(all)
     num += (sprite_index == argument[0]);
return num;
because sprite_index == argument[0] is '1' if true and '0' if false then you can use it in this way.
if that's too weird for you then you can stick with an if statment:
Code:
/// sprite_number(spr);
var num = 0;
with(all)
      if(sprite_index == argument[0]) num++;
return num;
There isn't much point in iterating over every object using a for loop and instance_number / instance_find. Takes more code to write, more things happening behind the scenes, and (likely) more processing time.

NOTE i chose to use no brackets in my examples because if there is only one line following there is no need to have them and it appears cleaner. Sometimes that confuses people.
 
B

Bhreno

Guest
This script should do it
It loops through all instances and checks if the object's sprite is the one you want to check. If it is it adds 1. Then returns total
Code:
/// sprite_number(spr)
var num = 0;
for(var i = 0; i < instance_number(all); i++)
    {
    var inst = instance_find(all, i);
    if inst.sprite_index == argument0 num++;
    }
return num;
Could I use this code in the Step Event without var counting "+1" in each step?
 

CloseRange

Member
Could I use this code in the Step Event without var counting "+1" in each step?
the var keyward means that the variable is cleared at the end of the calling script / code block.
running any of the scripts from the step event will provide you with the correct number regardless.

create a script called sprite_number
put the code in the script and then you can call it any time:
Code:
yellow_ball = sprite_number(spr_yellow_ball); // tells you how many yellow yellow ball sprites are in the room.
EDIT: Something I am just now realizing is that you double posted this question. Please refrain from doing that in the future as it's against the forum policy or what-not
 
B

Bhreno

Guest
So would my code look like this?

In obj_game BEGIN STEP EVENT

Code:
/// sprite_number(spr);
var default_ball = 0;
var yellow_ball = 0;
var blue_ball = 0;
with(obj_ball)
     if (sprite_index == spr_default_ball) default_ball++;
     if (sprite_index == spr_yellow_ball) yellow_ball++;
     if (sprite_index == spr_blue_ball) blue_ball++;
return default_ball, yellow_ball, blue_ball;
default_ball > to count obj_ball spr_default_ball.
yellow_ball > to count obj_ball spr_yellow_ball.
blue_ball > to count obj_ball spr_blue_ball.
 

TailBit

Member
If you need to use those counters outside the script then I wouldn't put var in front of them

Your with is not working as you ecpect here, as only the first if is included in it, this could be solved by replacing the ";" with "else" or using { } around all the three ifs you want inside the with

returns only returns one value, but that that could be a reference to something holding more values, like an array, instance or data structure.

Code:
default_ball = 0;
yellow_ball = 0;
blue_ball = 0;

with(obj_ball)
     if (sprite_index == spr_default_ball) default_ball++ else
     if (sprite_index == spr_yellow_ball) yellow_ball++ else
     if (sprite_index == spr_blue_ball) blue_ball++;
That could go directly in your step event, as it resets the counters before going over them again
 
B

Bhreno

Guest
If you need to use those counters outside the script then I wouldn't put var in front of them

Your with is not working as you ecpect here, as only the first if is included in it, this could be solved by replacing the ";" with "else" or using { } around all the three ifs you want inside the with

returns only returns one value, but that that could be a reference to something holding more values, like an array, instance or data structure.

Code:
default_ball = 0;
yellow_ball = 0;
blue_ball = 0;

with(obj_ball)
     if (sprite_index == spr_default_ball) default_ball++ else
     if (sprite_index == spr_yellow_ball) yellow_ball++ else
     if (sprite_index == spr_blue_ball) blue_ball++;
That could go directly in your step event, as it resets the counters before going over them again
Thank you all! Your code worked perfectly!
 
Top