Gw Temp

Menu

Tutorial - 'Lighting Effects in GM5' by The_Pup

An item about Gamemaker posted on

Blurb

The_Pup writes another great Game Maker tutorial that reveals the mystery of its lighting effects.

Body

Lighting Effects

Although this tutorial is a new work, it was derived from some old example I created that was stuck on my computer for a few *cough* months. Unless you want to know what I'm currently working on yet don't want to get the [horrible] full picture of my hentai-filled hard-drive then here's a look: DOS being programmed in GML. But in more recent news there has been news of GM 6.x series coming out soon which, from the screenshots, looks good with all the 3D enhancements and improvements. But I'm not gonna work with that because I'm too cheap. Now I'm gonna cut the feces and get right to the point.

This tutorial is targetted towards the beginner-intermediate crowd of users who use Game Maker version 5.x(though it is based on version 5.0). Things you should know will be explained in the tutorial and if you have any questions, comments, corrections, and/or then don't hesitate to go to the little comment box at the bottom of the page.

----------

i. Before we start

Before we begin this tutorial we need to set the Exclusive Graphics mode on because this tutorial explains a feature that is dependant on this mode. If you already know how to do this then skip this section.

First, press and hold 'Ctrl' + 'Alt' + 'G' or go into the Add menu and select 'Game Options.' Next, click on the 'Resolution' tab then click the 'Set resolution of the screen' checkbox and finally click on the checkbox with the label of 'Use exclusive graphics mode.'

To be safe lets go into the 'Keys' tab and check the checkbox 'Let end the game.' Do this so we won't get stuck in Exclusive Graphics mode forever.

If you don't know what Exclusive Graphics mode is then here's an explaination: This mode allows only the game window to be seen without any disturbance from other programs. Some features of GM can only be used if Exclusive Graphics mode is on but some other features, such as the popup functions, cannot be used in Exclusive Graphics mode.

-----

ii. Image Alpha

Image alpha, to be put simply, is transparency. In Game Maker it is held in a variable called 'image_alpha' which could have a value from 0(fully transparent) to 1(fully visible). Note that you can use Image Alpha with or without Exclusive Graphics mode. Image Apha only affects the non-transparent areas of a sprite. An example of how to make a transparent sprite so you can see through it:

//Start GML for half transparency

image_alpha = 0.5

//End GML

To make this even better, we could have a dark background an make use of this command by making objects and light sources.

First make a lightbulb sprite that is 32 by 32. Make an object called 'lightsource' with no events(later, you could uncheck the 'Visible' box, but keep the box checked so you can see the lights). Then create an object called 'moveable'(with a different sprite than 'lightsource' and make it able to move in 4 directions by keyboard. In the Step Event of 'moveable' put in this code:

//Start GML

image_alpha = distance_to_object(lightsource)/100 //100 how far the 'light' will radiate

//End GML

Now create a room and change it's background to have a dark background image or color. Place one instance of object 'moveable' in the room along with around 5 instances of object 'lightsource.'

Run the game and move object 'moveable' around, away, and towards the various 'lightsource' objects. Note that the 'lightsource' object was just a placeholder.

-----

iii. Screen Gamma

Screen Gamma is the reason we set Exclusive Graphics mode in the beginning of this tutorial. For all those people who like using RGB(Red, Green, Blue) values then you'll love using Screen Gamma.

The function is 'screen_gamma(r,g,b)' where r, g, and b are the Red, Green, and Blue hues being applied to the screen. The values of RGB cannot be over 1(lightest) or below -1(darkest). When you play any game the values of RGB are all 0. Anything above 0 makes the screen lighter and anything below 0 is darker. In it's simplest form of setting a colour to a game you could just use it like this:

//Start GML set lighting to red

screen_gamma(1,0,0) //Red = 1, Green = 0, Blue = 0

//End GML

A simple script to fade the screen to black would be as followed:

//Start GML Fade to black

rgb = 0;

repeat (21)
{
screen_gamma(rgb,rgb,rgb);
sleep(125); //If we didn't have this, it'd just automatically turn black
rgb -= 0.05 //This line does all the fading
}

//End gml

If you changed the line 'rgb -= 0.05' to 'rgb += 0.05' then you would fade to a white screen. Just a little change would've made alot of difference.

Yet you could fade out/in to/from different colours too by making separate values for red, green, and blue:

//Start GML fade to some odd colour

r = 0;
g = 0;
b = 0;

repeat (21)
{
screen_gamma(r,g,b);
sleep(125);
r += 0.05; //Increase value of red
g -= 0.09; //Decrease value of green
b += 0.00; //Don't change value of blue
}

//End GML

-----

iv. End

There are many uses for these two features of Game Maker 5. For one, you could use it to make an ambient setting in your game like a flashback or nighttime/daytime effects. You could use it to ad an element to gameplay like a cloak for enemies or players in online competition. By far, these features are great, if used correctly.

ciao,
Shaun