• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

This code works, but why? draw_set_alpha at the end

Bentley

Member
Hello. Just a heads up, I'm a beginner.

I made a room transition object that draws a rectangle over the screen. It fades in in room 1, and then fades out in room 2. Here's the code. (I'm sorry for the lack of indentation. After I posted, the indentation was removed).

draw_set_alpha( alpha );
draw_rectangle( 0, 0, room_width, room_height, false );


if( fade == "in" )
{
if( alpha < 1 ) alpha += 0.01;
else
{
alpha = 1;
room_goto_next();
fade = "out";
}
}


if( fade == "out" )
{
if( alpha > 0 ) alpha -= 0.01;
else instance_destroy();
}


draw_set_alpha( 1 );

If I understand the Draw event correctly (which I'm sure I don't), it runs every step, and it also runs every line of code every step. So going off that bad logic, why doesn't the last line, which defaults opacity to 1, not turn the screen black at the end of that step? (And at the start of the next step, draw_set_alpha( alpha ) would set opacity back to the value of the variable alpha).

Thank you for reading. By the way, if you see anything unrelated that I can improve on, please let me know.
 
Last edited:

obscene

Member
You might need to rephrase that, question doesn't make sense :p

Why WOULD draw_set_alpha(1) interrupt anything? It just sets the draw alpha to 1.

Is all that code in the draw event or is part in the step?
 

Bingdom

Googledom
You set the alpha back to 1 after everything has been drawn. If you want it to effect the rectangle, put it after draw_set_alpha(alpha) and before draw_rectangle()

Btw, use code blocks [ code ] [ /code ] (without spaces)
 

Bentley

Member
You might need to rephrase that, question doesn't make sense :p

Why WOULD draw_set_alpha(1) interrupt anything? It just sets the draw alpha to 1.

Is all that code in the draw event or is part in the step?
Thanks for the reply. Yes, the code is in the Draw.

Tell me if I'm wrong here: alpha += 0.01 occurs once every step right? And as GM continues down and gets to draw_set_alpha( 1 ), that also occurs once in that same step. So going off that bad logic, why doesn't the last line, which defaults opacity to 1, not turn the screen black at the end of that step? (And at the start of the next step, draw_set_alpha( alpha ) would set opacity back to the value of the variable alpha).

Ugh, I've gone cross-eyed lol.
 
Last edited:

Bentley

Member
You set the alpha back to 1 after everything has been drawn. If you want it to effect the rectangle, put it after draw_set_alpha(alpha) and before draw_rectangle()

Btw, use code blocks [ code ] [ /code ] (without spaces)
Thanks for the reply. I appreciate the tip about no spaces between code blocks.
 
W

Wraithious

Guest
I believe it's because you've allready drawn after setting the alpha via your alpha variable and then at the very end, you're setting alpha to 1, then at the beginning of the next step before you draw you're setting the alpha acording to the code you are feeding it in line 1 (of your original post) so essentially if thats a regular draw code the only thing that would be affected to your last line of that code would be a begin draw event ( if you have one ) anywhere in an active instance that draws.

To test that just add a pre draw event and put
 
Last edited by a moderator:

Murzy

Member
Code:
draw_set_alpha(1.0);
Is just there to reset the alpha for the following draw calls for the GMS engine.

It is basically the same as this
Code:
var alpha_before = draw_get_alpha();

// FANCY DRAW CODE THAT PLAYS WITH THE ALPHA VALUE HERE

draw_set_alpha(alpha_before);
When you once forget to reset the alpha when doing something like this, you'll realize why its there. When, for example, the rest of the sprites in your game start being drawn with alpha value of 0.5 etc...
 
Top