Marc, after looking at how you are using the lightning, there might be a more efficient and easier way of doing this.

You could have each cloud check if the variable "lightning" is set, if it is, then a random test would control how frequent a bolt is made. Finally the cloud would call a function to throw a lightning bolt from itself to the nearest designated target.

This would be much simpler and efficient than the current method. No need for entity pointers. Although the target would always be the nearest in the strike range and not a random target in the strike range. I think the result would most times be the same.

Heres how I might do it;

Code:
 
//================================================================================
// VARIABLES & SOUNDS
//================================================================================


var lightning_on = 1; // use this variable to turn lightning on or off
var lightning_freq = 0.985; // frequentcy of lightning strikes, 0 = constant, 1 = never
var strike_range = 1000; // how close cloud origin must be to target origin for a strike
var flash_range = 1000; // lightrange for the lightning flash
var thunder_snd_range = 800; // set range of 3d sounds here


var thunder_choice; //sound choice variable
sound thunder1 = "thunder1.wav";
sound thunder2 = "thunder2.wav";
sound thunder3 = "thunder3.wav";


//================================================================================
// LIGHTNING CLOUD ACTION
//================================================================================


Action cloud
{
while(me)
{
if(lightning_on == 1)
{
if (random(1) > lightning_freq ) {lightning_strike();}
}
wait(1);
}
}


//================================================================================
// LIGHTNING TARGET ACTION
//================================================================================


Action building //or any lightning target!
{
my.skill1 = 2; // identifies entity as a strike target
}


//================================================================================
// LIGHTNING_STRIKE FUNCTION
//================================================================================


function lightning_strike()
{
var closest_target;
closest_target = strike_range + 1; // initally set just outside of strike range
vec_set(line_start.x, my.x); // set calling ent as start vec

you = ent_next(null);
while ( you != 0 ) // cycle through entitys
{
if(you.skill1 == 2) // if you are a target
{
temp = vec_dist(my.x, you.x); // get the distance
if(temp < closest_target) // if you are closer than last closest target
{
vec_set(line_end.x, you.x); // set you as end vec
closest_target = temp; // set you as new closest target
}
}
you = ent_next(YOU); // get next entity
}

if(closest_target <= strike_range) // if the closest target is now within range, make lightning
{
lightning_effect();

vec_set(my.blue, vector(255,128,128)); // set amount of blue,green and red in flash here
my.light = on;
my.lightrange = flash_range;
wait(2);

thunder_choice = int(random(6));
if(thunder_choice == 0 || thunder_choice == 3){ent_playsound(my, thunder1, thunder_snd_range);}
if(thunder_choice == 1 || thunder_choice == 4){ent_playsound(my, thunder2, thunder_snd_range);}
if(thunder_choice == 2 || thunder_choice == 5){ent_playsound(my, thunder3, thunder_snd_range);}

my.light = off;
my.lightrange = 0;
}
}




You could even change this to find the three or so nearest targets, and randomly pick one, but I believe that real lightning jumps to the nearest target anyway so perhaps this will be fine.

Hope this helps with your project in someway, even if only in new ideas. Keep going!


Not two, not one.