Legacy GM Lighting help

V

Vanthex

Guest
Hi guys,
I posted on gamemaker's reddit about a lighting system that I wanted that looks like this:

In the replies I received, one of them provided me help on the code which is this:
SCRIPT (scr_set_block_alpha):

Code:
///scr_set_block_alpha()

var a = 0;

for (var i = 1; i <= 4; i++) {
    if (place_meeting(x, y - (sprite_height * i), obj_block)) {
        a += 1;
    }
}

if (a > 0) {
    a = (a * 25) / 100;
}

return a;
Now we'll return an alpha value as low as 0 if there are no blocks above our block or as high as 1 if there are at least 4 blocks above our block. This decreases as there are less blocks, so 3 blocks above us returns 0.75, 2 blocks is 0.50, and so on...this gets the transparency we want for our darkness.

Next, we should head into the Create Event for the block and set our transparency using the custom script right there.

CREATE EVENT:

Code:
alpha = scr_set_block_alpha();
All we have to do now is draw our block sprite and draw it again in black, but with the transparency value we calculated in the variable alpha.

DRAW EVENT:
Code:
//Draw self
draw_self();

//Draw darkness
draw_sprite_ext(sprite_index, image_index, x, y, 1, 1, 0, c_black, alpha);
Now the lighting works, but how do I make it so that it constantly updates when I create or destroy an instance? Do I need to create new events?
 

trg601

Member
Well if you simply wanted to update all instances when you created or destroyed a block, just put
"with(block_object){scr_set_block_alpha()}" whenever in the create event and the destroy event of the block objects.
(Or just put it in a parent object and run event_inherited() for all the blocks.)

It would be more efficient to just run it under all objects in the same vertical line as you placed/destroyed the block.
 
Top