Graphics Drawing lightning in your game

A

amusudan

Guest

Screenshot from my game.

GM Version
: GameMaker: Studio
Target Platform: All
Download: New download here, fixed one error with a non-existant script.
Links: N/A

Summary:
Ever wanted to draw kick-ass lightning in your game but aren't the artsy type? Generate it! With this simple script you can draw wicked lightning, wickedly easy!

Tutorial:
Download the script and import it into your project by right clicking on the "scripts" folder and clicking on "add existing script".



Then call the script like this:
draw_lightning(x, y, x2, y2, branches, colour)
Where x + y make up the first point of your lightning and x2 + y2 make up the final point of your lightning.
Branches is wether the lighting splits up into smaller branches of lightning and colour is the colour of the glow around the lightning.

If you have any problems just comment it down below, I hope this helped you in making your game a little bit awesomer :).
 
Last edited:
S

Spacedog

Guest
I tried this but the script references another script called scr_lightning which is not included so it won't work. Would like to try it though, been looking for something like this.
 
D

Davemane42

Guest
I have a small problem with your script. At line 61 it refer to scr_lightning who is none existant and the script is unusable. thank for awsering me.
 
A

amusudan

Guest
Oh, sorry about that! Just change scr_lightning to draw_lightning (and add the colour variable to it), I'll update the file too!

Terribly sorry for this!
 
Last edited:
S

SoulTie

Guest
This looks pretty good. I have added an argument to the script for the "size" variable; I think that it looks better with a thicker line. Maybe you could add this to the download? Good job
 
A

amusudan

Guest
This looks pretty good. I have added an argument to the script for the "size" variable; I think that it looks better with a thicker line. Maybe you could add this to the download? Good job
Yes that does sound like a good idea, I'll update the file as soon as I can open GM:S :).
 
G

GMSNormie

Guest

Screenshot from my game.

GM Version
: GameMaker: Studio
Target Platform: All
Download: New download here, fixed one error with a non-existant script.
Links: N/A

Summary:
Ever wanted to draw kick-ass lightning in your game but aren't the artsy type? Generate it! With this simple script you can draw wicked lightning, wickedly easy!

Tutorial:
Download the script and import it into your project by right clicking on the "scripts" folder and clicking on "add existing script".



Then call the script like this:
draw_lightning(x, y, x2, y2, branches, colour)
Where x + y make up the first point of your lightning and x2 + y2 make up the final point of your lightning.
Branches is wether the lighting splits up into smaller branches of lightning and colour is the colour of the glow around the lightning.

If you have any problems just comment it down below, I hope this helped you in making your game a little bit awesomer :).
Super late, but I can't open the GML file in GMS, it says it has to be in .yy format? Is this because I'm using GMS2?
 
A

amusudan

Guest
Super late, but I can't open the GML file in GMS, it says it has to be in .yy format? Is this because I'm using GMS2?
Sorry this was made in GMS1, and I do not own version 2 so I can't make an updated version for you :<
 

rIKmAN

Member
Super late, but I can't open the GML file in GMS, it says it has to be in .yy format? Is this because I'm using GMS2?
Try opening the script with a text editor and copying the code.
Then create a new script in GMS2 and paste the code into script editor.

There may be a few changes you need to make (or maybe not), but it shouldn't be too hard to make it work as most functions are the same between versions.
 
Great effect. But, is there a way to make it stop drawing when the bolt reaches de x2 (max distance), like in your new example? Thanks!
 
A

Art2mis

Guest
Awesome script! Please do notify me when the new and improved version is released!
 

gnysek

Member
Could you post the code so I can convert it to GML2?
From what I see file from first post requires login to Dropbox, so here's it's content, you need to enclose it in function() of course for GMS 2.3+
GML:
#define draw_lightning
///draw_lightning(x, y, x2, y2, branches, colour)
//
//draws a lightning bolt from the given starting location to the given end location
//
//x = x of the bolt's start
//y = y of the bolt's start
//x2 = x of the bolt's end
//y2 = y of the bolt's end
//branches = true or false, if the lightning bolt branches into multiple smaller ones
//colour = colour of the glow
//
//amusudan 23/5/2016
//
//feel free to use this in your project!
//
var dir = point_direction(argument0,argument1,argument2,argument3);
var length = point_distance(argument0,argument1,argument2,argument3);
var colour = argument5;

//make different segments
var i = 0;
var i2 = 1;
var point;
point[0] = 0;
do
    {
    i++;
    if random(1) < .06
        {
        point[i2] = i;
        i2++;
        }
    }
until i >= length
point[i2] = length;
var points = array_length_1d(point);

//draw segments
var i = 0;
var i2 = 1
var difx = 0;
var difx2 = 0;
var dify = 0;
var dify2 = 0;

do //loop through and draw all the segments
    {
    difx = random_range(-7,7)
    dify = random_range(-7,7)
    
    var xx = argument0 + lengthdir_x(point[i2 - 1],dir);
    var yy = argument1 + lengthdir_y(point[i2 - 1],dir);
    var xx2 = argument0 + lengthdir_x(point[i2],dir);
    var yy2 = argument1 + lengthdir_y(point[i2],dir);
    
    //create a branch
    if random(1) < .15 && argument4
        {
        var bdir = dir + choose(random_range(-45,-25),random_range(45,25));
        var blength = random_range(5,30);
        draw_lightning(xx + difx2, yy + dify2, xx + difx2 + lengthdir_x(blength,bdir), yy + dify2 + lengthdir_y(blength,bdir), false,colour)
        }
    
    var size = random_range(.1,1);
    //draw the glow of the lightning
    draw_set_alpha(.1)
    draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 5,colour,colour);
    draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 3,c_white,c_white);
    draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 1,c_white,c_white);
    draw_set_alpha(1)
    //draw the white center of the lightning
    draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size,c_white,c_white);
    
    i2++;
    difx2 = difx;
    dify2 = dify;
    }
until i2 = points

//draw a glowing circle
if argument4
    {
    var size = random_range(1,2)
    draw_set_alpha(.1)
    draw_circle_colour(argument0,argument1,size + 2.5,colour,colour,false);
    draw_circle_colour(argument0,argument1,size + 1.5,colour,colour,false);
    draw_circle_colour(argument0,argument1,size + .5,colour,colour,false);
    draw_set_alpha(1)
    draw_circle_colour(argument0,argument1,size,c_white,c_white,false);
    }
 

bandman28

Member
From what I see file from first post requires login to Dropbox, so here's it's content, you need to enclose it in function() of course for GMS 2.3+
GML:
#define draw_lightning
///draw_lightning(x, y, x2, y2, branches, colour)
//
//draws a lightning bolt from the given starting location to the given end location
//
//x = x of the bolt's start
//y = y of the bolt's start
//x2 = x of the bolt's end
//y2 = y of the bolt's end
//branches = true or false, if the lightning bolt branches into multiple smaller ones
//colour = colour of the glow
//
//amusudan 23/5/2016
//
//feel free to use this in your project!
//
var dir = point_direction(argument0,argument1,argument2,argument3);
var length = point_distance(argument0,argument1,argument2,argument3);
var colour = argument5;

//make different segments
var i = 0;
var i2 = 1;
var point;
point[0] = 0;
do
    {
    i++;
    if random(1) < .06
        {
        point[i2] = i;
        i2++;
        }
    }
until i >= length
point[i2] = length;
var points = array_length_1d(point);

//draw segments
var i = 0;
var i2 = 1
var difx = 0;
var difx2 = 0;
var dify = 0;
var dify2 = 0;

do //loop through and draw all the segments
    {
    difx = random_range(-7,7)
    dify = random_range(-7,7)
  
    var xx = argument0 + lengthdir_x(point[i2 - 1],dir);
    var yy = argument1 + lengthdir_y(point[i2 - 1],dir);
    var xx2 = argument0 + lengthdir_x(point[i2],dir);
    var yy2 = argument1 + lengthdir_y(point[i2],dir);
  
    //create a branch
    if random(1) < .15 && argument4
        {
        var bdir = dir + choose(random_range(-45,-25),random_range(45,25));
        var blength = random_range(5,30);
        draw_lightning(xx + difx2, yy + dify2, xx + difx2 + lengthdir_x(blength,bdir), yy + dify2 + lengthdir_y(blength,bdir), false,colour)
        }
  
    var size = random_range(.1,1);
    //draw the glow of the lightning
    draw_set_alpha(.1)
    draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 5,colour,colour);
    draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 3,c_white,c_white);
    draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 1,c_white,c_white);
    draw_set_alpha(1)
    //draw the white center of the lightning
    draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size,c_white,c_white);
  
    i2++;
    difx2 = difx;
    dify2 = dify;
    }
until i2 = points

//draw a glowing circle
if argument4
    {
    var size = random_range(1,2)
    draw_set_alpha(.1)
    draw_circle_colour(argument0,argument1,size + 2.5,colour,colour,false);
    draw_circle_colour(argument0,argument1,size + 1.5,colour,colour,false);
    draw_circle_colour(argument0,argument1,size + .5,colour,colour,false);
    draw_set_alpha(1)
    draw_circle_colour(argument0,argument1,size,c_white,c_white,false);
    }
What does #define mean?
 
Last edited:

kburkhart84

Firehammer Games
What does #define mean?
The function was made in older versions of GM/GMS. The define line is something that was added to script files I think. You can just remove it. Or actually I have a version I fixed for my jam game so you can just use it as is if you just copy/paste it into GMS2.3+.

Code:
function drawLightning(x1, y1, x2, y2, branches, color)
{
    ///draw_lightning(x, y, x2, y2, branches, colour)
    //
    //draws a lightning bolt from the given starting location to the given end location
    //
    //x = x of the bolt's start
    //y = y of the bolt's start
    //x2 = x of the bolt's end
    //y2 = y of the bolt's end
    //branches = true or false, if the lightning bolt branches into multiple smaller ones
    //colour = colour of the glow
    //
    //amusudan 23/5/2016
    //
    //feel free to use this in your project!
    //
    var dir = point_direction(x1,y1,x2,y2);
    var length = point_distance(x1,y1,x2,y2);
    var colour = color;

    //make different segments
    var i = 0;
    var i2 = 1;
    var point;
    point[0] = 0;
    do
        {
        i++;
        if random(1) < .06
            {
            point[i2] = i;
            i2++;
            }
        }
    until i >= length
    point[i2] = length;
    var points = array_length_1d(point);

    //draw segments
    var i = 0;
    var i2 = 1
    var difx = 0;
    var difx2 = 0;
    var dify = 0;
    var dify2 = 0;

    do //loop through and draw all the segments
        {
        difx = random_range(-7,7)
        dify = random_range(-7,7)
    
        var xx = x1 + lengthdir_x(point[i2 - 1],dir);
        var yy = y1 + lengthdir_y(point[i2 - 1],dir);
        var xx2 = x1 + lengthdir_x(point[i2],dir);
        var yy2 = y1 + lengthdir_y(point[i2],dir);
    
        //create a branch
        if random(1) < .15 && branches
            {
            var bdir = dir + choose(random_range(-45,-25),random_range(45,25));
            var blength = random_range(5,30);
            drawLightning(xx + difx2, yy + dify2, xx + difx2 + lengthdir_x(blength,bdir), yy + dify2 + lengthdir_y(blength,bdir), false,colour)
            }
    
        var size = random_range(.1,1);
        //draw the glow of the lightning
        draw_set_alpha(.1)
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 5,colour,colour);
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 3,c_white,c_white);
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 1,c_white,c_white);
        draw_set_alpha(1)
        //draw the white center of the lightning
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size,c_white,c_white);
    
        i2++;
        difx2 = difx;
        dify2 = dify;
        }
    until i2 = points

    //draw a glowing circle
    if branches
        {
        var size = random_range(1,2)
        draw_set_alpha(.1)
        draw_circle_colour(x1,y1,size + 2.5,colour,colour,false);
        draw_circle_colour(x1,y1,size + 1.5,colour,colour,false);
        draw_circle_colour(x1,y1,size + .5,colour,colour,false);
        draw_set_alpha(1)
        draw_circle_colour(x1,y1,size,c_white,c_white,false);
        }
}
 

bandman28

Member
The function was made in older versions of GM/GMS. The define line is something that was added to script files I think. You can just remove it. Or actually I have a version I fixed for my jam game so you can just use it as is if you just copy/paste it into GMS2.3+.

Code:
function drawLightning(x1, y1, x2, y2, branches, color)
{
    ///draw_lightning(x, y, x2, y2, branches, colour)
    //
    //draws a lightning bolt from the given starting location to the given end location
    //
    //x = x of the bolt's start
    //y = y of the bolt's start
    //x2 = x of the bolt's end
    //y2 = y of the bolt's end
    //branches = true or false, if the lightning bolt branches into multiple smaller ones
    //colour = colour of the glow
    //
    //amusudan 23/5/2016
    //
    //feel free to use this in your project!
    //
    var dir = point_direction(x1,y1,x2,y2);
    var length = point_distance(x1,y1,x2,y2);
    var colour = color;

    //make different segments
    var i = 0;
    var i2 = 1;
    var point;
    point[0] = 0;
    do
        {
        i++;
        if random(1) < .06
            {
            point[i2] = i;
            i2++;
            }
        }
    until i >= length
    point[i2] = length;
    var points = array_length_1d(point);

    //draw segments
    var i = 0;
    var i2 = 1
    var difx = 0;
    var difx2 = 0;
    var dify = 0;
    var dify2 = 0;

    do //loop through and draw all the segments
        {
        difx = random_range(-7,7)
        dify = random_range(-7,7)
   
        var xx = x1 + lengthdir_x(point[i2 - 1],dir);
        var yy = y1 + lengthdir_y(point[i2 - 1],dir);
        var xx2 = x1 + lengthdir_x(point[i2],dir);
        var yy2 = y1 + lengthdir_y(point[i2],dir);
   
        //create a branch
        if random(1) < .15 && branches
            {
            var bdir = dir + choose(random_range(-45,-25),random_range(45,25));
            var blength = random_range(5,30);
            drawLightning(xx + difx2, yy + dify2, xx + difx2 + lengthdir_x(blength,bdir), yy + dify2 + lengthdir_y(blength,bdir), false,colour)
            }
   
        var size = random_range(.1,1);
        //draw the glow of the lightning
        draw_set_alpha(.1)
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 5,colour,colour);
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 3,c_white,c_white);
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 1,c_white,c_white);
        draw_set_alpha(1)
        //draw the white center of the lightning
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size,c_white,c_white);
   
        i2++;
        difx2 = difx;
        dify2 = dify;
        }
    until i2 = points

    //draw a glowing circle
    if branches
        {
        var size = random_range(1,2)
        draw_set_alpha(.1)
        draw_circle_colour(x1,y1,size + 2.5,colour,colour,false);
        draw_circle_colour(x1,y1,size + 1.5,colour,colour,false);
        draw_circle_colour(x1,y1,size + .5,colour,colour,false);
        draw_set_alpha(1)
        draw_circle_colour(x1,y1,size,c_white,c_white,false);
        }
}
Oh ok thanks looks like nothing really was needed to be done.
 

bandman28

Member
There wasn't much to do, since most of the functions were the same, but I added a size variable in the function bracket so that the user can change the size, and I changed the do, until functions to for loops.
Here's draw_lightning for GMS 2.3+:
GML:
function draw_lightning(x1, y1, x2, y2, branches, size, color) {
    ///draw_lightning(x, y, x2, y2, branches, size, colour)
    //
    //draws a lightning bolt from the given starting location to the given end location
    //
    //x = x of the bolt's start
    //y = y of the bolt's start
    //x2 = x of the bolt's end
    //y2 = y of the bolt's end
    //branches = true or false, if the lightning bolt branches into multiple smaller ones
    //size = pixel width of the lightning
    //colour = colour of the glow
    //
    //amusudan 23/5/2016
    //
    //feel free to use this in your project!
    //
    var dir = point_direction(x1,y1,x2,y2);
    var length = point_distance(x1,y1,x2,y2);
    var colour = color;
    var _size = size;

    //make different segments
    var point;
    point[0] = 0;
    var i2 = 1;
    for (var i = 0; i < length; i++) {
        if (random(1) < .06) {
            point[i2] = i;
            i2++;
        }
    }
    point[i2] = length;
    var points = array_length(point);

    //draw segments
    var i2 = 1
    var difx = 0;
    var difx2 = 0;
    var dify = 0;
    var dify2 = 0;
    for (i2 = 1; i2 < points; i2++) {
        difx = random_range(-7,7)
        dify = random_range(-7,7)
    
        var xx = x1 + lengthdir_x(point[i2 - 1],dir);
        var yy = y1 + lengthdir_y(point[i2 - 1],dir);
        var xx2 = x1 + lengthdir_x(point[i2],dir);
        var yy2 = y1 + lengthdir_y(point[i2],dir);
    
        //create a branch
        if (random(1) < .15 && branches) {
            var bdir = dir + choose(random_range(-45,-25),random_range(45,25));
            var blength = random_range(5,30);
            draw_lightning(xx + difx2, yy + dify2, xx + difx2 + lengthdir_x(blength,bdir), yy + dify2 + lengthdir_y(blength,bdir), false, _size, colour)
           }
    
        //draw the glow of the lightning
        draw_set_alpha(.1)
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 5,colour,colour);
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 3,c_white,c_white);
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 1,c_white,c_white);
        draw_set_alpha(1)
        //draw the white center of the lightning
        draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size,c_white,c_white);
    
        i2++;
        difx2 = difx;
        dify2 = dify;
    }

    //draw a glowing circle
    if (branches) {
        draw_set_alpha(.1);
        draw_circle_colour(x1,y1,size + 2.5,colour,colour,false);
        draw_circle_colour(x1,y1,size + 1.5,colour,colour,false);
        draw_circle_colour(x1,y1,size + .5,colour,colour,false);
        draw_set_alpha(1);
        draw_circle_colour(x1,y1,size,c_white,c_white,false);
        }
}
 
Just wanted to say thanks to everyone for the code. I was settling with a simple draw_line effect which was okay because it happened so fast that it still managed to be flashy, but an actual lightning effect is way more fitting and I love learning how it's done by looking at the code.

Much appreciated!
 
Top