This code works, but why? draw_set_alpha at the end

Discussion in 'Programming' started by Bentley, Jun 29, 2017.

Tags:
  1. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    798
    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: Jun 29, 2017
  2. obscene

    obscene Member

    Joined:
    Jun 21, 2016
    Posts:
    2,428
    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?
     
    Bentley likes this.
  3. Bingdom

    Bingdom Googledom

    Joined:
    Jul 1, 2016
    Posts:
    1,678
    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 likes this.
  4. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    798
    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: Jun 29, 2017
  5. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    798
    Thanks for the reply. I appreciate the tip about no spaces between code blocks.
     
  6. Wraithious

    Wraithious Member

    Joined:
    Jun 24, 2016
    Posts:
    1,166
    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: Jun 29, 2017
    Bentley likes this.
  7. Murzy

    Murzy Member

    Joined:
    Jul 28, 2016
    Posts:
    24
    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...
     
    Bentley likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice