Hello Marc, sorry I didnt get here sooner.

your problem is not with the "lightning_effect" function, but with the distance of the
start/end vectors you are supplying it with, (a distance of zero between them is causing
the error). This is because the cloud vec or target is being used to set both the start
AND end vectors so the distance is zero, or the cloud pointer is null and causing a
similar problem.

This is probably caused in part by the line "not_cloud = you". This was an editing
mistake I overlooked. it should have been "cloud != you".

I would keep the lightning effect function as is. If you want to prevent zero errors,
(which you shouldnt have anymore anyway), instead of using "line_length = 1;" insert
a check after the line_length is calculated like this;

Code:
 
line_length = vec_length(line_end); // total length of bolt
if(line_length == 0){line_length = 1;} // NEW LINE GOES HERE



I edited the original code a little to fix the problems and added some comments to explain
it better. Here is the result;


Code:
  

//================================================================================
// Lightning Strike Function
//================================================================================

var strike_dist = 325; // maximun distance in quants lightning will strike,(how close the cloud must be)
var flash_dist = 300; // set lightrange of light flash here
var snd_dist = 800; // set range of 3d sounds here

var pos_pick;
var neg_pick;
var num_of_pos_ents = 0; //holds number of positive assigned ents
var num_of_neg_ents = 0;

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

entity* cloud;


function lightning_strike()
{
var counter;
pos_pick = int(random(num_of_pos_ents)+1); //get a random pos ent number (pick a cloud #)
neg_pick = int(random(num_of_neg_ents)+1); //get a random neg ent number (pick a building #)

counter = 1;
//cycle through entitys until all clouds are processed
you = ent_next(null); //get first entity
while(you != 0 && (counter <= num_of_pos_ents))
{
if(you.skill1 == 1) //if you are a pos ent (cloud)
{
if(counter == pos_pick)//if you are our picked cloud
{
vec_set(line_start.x, you.x); //set you as the start vec
cloud = you; // we make you current cloud
}
else //you are NOT our picked cloud
{
cloud != you; //remove pointer if assign previously
}
counter += 1; // increment pos counter
}
you = ent_next(YOU); //get next entity
}

counter = 1; //reset counter
//cycle through entitys untill all neg ents are processed
you = ent_next(NULL); //get first entity
while(you != 0 && (counter <= num_of_neg_ents))
{
if(you.skill1 == 2 )// if you are a neg ent
{
if(counter == neg_pick) //if you are our picked target
{
vec_set(line_end.x, you.x); //set you as the end vec
counter = num_of_neg_ents; //set counter so loop ends
}
counter += 1; // increment neg counter
}
you = ent_next(YOU); //get next entity
}

if(cloud) //avoid an empty pointer
{
if(vec_dist(line_start.x, line_end.x) < strike_dist )// strike only if closer than strike_dist
{
//create lightning bolt
lightning_effect();
//flash cloud
vec_set(cloud.blue, vector(255,128,128));// set amount of blue,green and red in flash here
cloud.light = on;
cloud.lightrange = flash_dist;
wait(2);

//play random thunder
thunder_choice = int(random(6)); // returns whole numbers 0 to 5
if(thunder_choice == 0 || thunder_choice == 3){ent_playsound(cloud, thunder1, snd_dist);}
if(thunder_choice == 1 || thunder_choice == 4){ent_playsound(cloud, thunder2, snd_dist);}
if(thunder_choice == 2 || thunder_choice == 5){ent_playsound(cloud, thunder3, snd_dist);}

//turn off cloud flash
cloud.light = off;
cloud.lightrange = 0;
}
}
}




I commented some lines to help explain it.
You may have some of the variables already defined, so you wont need to redefine them. However, with this rewrite, you dont need these any longer.

entity * not_cloud;
var lightning_on = 1;
var strike_freq = 0.8;
var pos_flag;
var neg_flag;

You could call this new function something like this
Code:
 
while(1)
{
if (random(1) > 0.95 ) // higher number less chance of a bolt
{
lightning_strike();
}
wait(1);
}



And if a cloud and building are chosen that are closer than the variable "strike_dist" , a bolt(s) will be thrown between them.

Hope this is all clear?


Not two, not one.