I managed to create some code, which switches on the nearest 7 or 8 dynamic lights, so if you are in range of 20 dynamic lights it will only turn on those closest to the player.

Not sure if this will help, I use pointer 'last_light_ent' and a variable 'current_lights', it also uses skill19 and another skill defined as 'my.light_visible_range' which is the range the player needs to be at to turn it on, the light also turns off if the camera is farther away from the light by a distance of light_visible_range * 1.5, to avoid some weird dynamic lights randomly appearing and staying stuck on all over the place...Otherwise it might be something hard to understand, good luck

ie from an entity function in a loop call:

handle_dynamic_lights(light_amount,pulsate_speed,pulsate_amount,random_pulse)
light_amount: the amount light_range becomes, such as 200
pulsate_speed: speed at which the light pulsates along a sin wave
pulsate_amount: how large the pulsation is
random_pulse: a random amount added onto light_range which creates flickering:

I use this for torches which I place on top of WED lights.
handle_dynamic_lights(200,80,12,20);

and I use this color:
my.blue = 10;
my.green = 30;
my.red = 40;

Here's that confuzzling code:
Code:

FUNCTION handle_dynamic_lights(light_amount,pulsate_speed,pulsate_amount,random_pulse) {
IF (last_light_ent == my && my.skill19 == 0) { last_light_ent = null; }
IF (vec_dist(vector(my.x,my.y,0),vector(player.x,player.y,0)) < my.light_visible_range) && (vec_dist(my.x,camera.x) < my.light_visible_range * 1.5) && (light_amount > 0) { //if we are in range
IF (my.skill19 == 0 && current_lights < 7) { //if we are turned off and there is enough lights left for us to turn on
my.skill19 = 1;
current_lights += 1;
}
IF (my.skill19 != 0 && last_light_ent != my) { if we have been turned on
IF (last_light_ent == null) { //if no entity is stored in last_light_ent make the my entity last_light_ent
last_light_ent = my;
} ELSE {
IF (vec_dist(my.x,player.x) > vec_dist(last_light_ent.x,player.x)) { //if last_light_ent already exists if this light is further away set this light to last_light_ent
last_light_ent = my;
}
}
}
IF (last_light_ent != null && last_light_ent != my && current_lights >= 7 && my.skill19 == 0) { //if dynamic lights are maxed out, this light is current turned off
IF (vec_dist(my.x,player.x) < vec_dist(last_light_ent.x,player.x)) { //if this light is closer than the light which is farthest from the player
last_light_ent.skill19 = 0; //turn off that light
last_light_ent.lightrange = 0;
last_light_ent = my;
my.skill19 = 1; //turn on this light
}
}
} ELSE {
IF (my.skill19 == 1) { my.skill19 = 3; } //if the light is on turn it off
}
IF (my.skill19 == 1) { //light is on
my.skill18 += pulsate_speed * time_step;
my.skill18 %= 360;
my.lightrange = light_amount + fsin(my.skill18,pulsate_amount) + random(random_pulse);
// my.lightrange += 0.1 * my.skill10 * time_step; //this was an attempt at having the my.skill19 == 1 state being the light slowly appearing and my.skill19 == 2 state being when the light is on
// IF (my.lightrange > my.skill10 + fsin(my.skill12,my.skill13)) { my.skill19 = 2; }
}
IF (my.skill19 == 3) { //turn the light off
IF (my.lightrange > 0) {
my.skill19 = 0; current_lights -= 1; my.lightrange = 0;
IF (last_light_ent == my) { last_light_ent = null; }
// my.lightrange -= 0.1 * pulsate_amount * time_step;
// IF (my.lightrange <= 0) { my.skill19 = 0; current_lights -= 1; my.lightrange = 0; }
}
}
}