SOLVED Counting instances that meet a variable

Foldup

Member
Hello my friends! I came across just one older thread on this subject but didn't quite get what I was looking for.

Trying to get instance_count but only of instances that have the variable met.

In my case, I want to count all o_socket where lit=0

Thoughts?
 

Gamebot

Member
Normally I don't use globals BUT... if its important to know you could just do this:

CREATE:
Code:
global.lit = 0;
In the code where lit = true:
Code:
global.lit++;
This way you can use it with other instance objects, scripts ect...as well.
 

Foldup

Member
Interesting thought, thanks.
FWIW, what I'm doing is running a line of electrically charged sockets and the meter at the end is supposed to count up how many are charged for an easy visual cue.

Part of the issue is that they change values throughout, so I need a count of how many currently are lit at any given time - a number that can go up and down.
 

chamaeleon

Member
GML:
var lit_counter = 0;
with (o_socket) {
    if (lit == 0)
        lit_counter++;
}
But as @Gamebot says, you can use a global counter as well. Wherever you change the lit instance variable is also a location where you can simultaneously change the global counter to reflect the correct state. What approach you pick is up to you.
 

Foldup

Member
Good thought! Thanks for that. I think you're probably on to a good approach there.

So, I just did a workaround where I use a Step event to periodically kick an Alarm so it just pulses a check instead of counting up in a Step event.

In the Alarm, I reset the Level and then count it up again.

GML:
mylevel=0
for (var i = x-96; i > x-(o_socket.sprite_width*8); i -= o_socket.sprite_width)
{
var inst;
inst = instance_place(i, y, o_socket);
if inst != noone
    {
    if inst.lit=1
    mylevel+=1
    }
}
It works, but I'll keep trying your approach too. Possibly use that in place of my instance_place ?
 

chamaeleon

Member
Your code doesn't quite indicate you wish to count *all* instances of o_socket so whether my code applies or not is not clear. Your first post did not have any additional conditions specified, hence my with() loop. If you want to additional constraints you can either place them inside the with() statement (in which case all instances will have these constraints applied), or you could use some other strategy (like you seemingly do in your code) for finding candidates (in which case my with() loop does not apply).
 

Foldup

Member
Yes, I did not wish to over-complicate the question. I'm counting up only the Sockets that are on the same X value as the power meter
 

chamaeleon

Member
Untested code (especially which x start and end coordinates to use for the line used to test for collision)
GML:
var lit_count = 0;
var sockets = ds_list_create();
var found = collision_line_list(x-96, y, x-(o_socket.sprite_width*8), y, o_socket, false, true, sockets, false);
var i = 0;
repeat found {
    if (sockets[| i].lit == 1)
        lit_count++;
    i++;
}
ds_list_destroy(sockets);
 
Top